can a static method be overridden
Answer Posted / konthoujam dhanabir singh
static method cannot be overriden to non-static.so static
method can be overriden to static.
the above example is true in static way
e.g.
class Animal {
static void doStuff() {
System.out.print("a ");
}
}
class Dog extends Animal {
static void dostuff() { // it's a redefinition,
// not an override
System.out.print("d ");
}
public static void main(String [] args) {
Animal [] a = {new Animal(), new Dog(), new Animal()};
for(int x = 0; x < a.length; x++)
a[x].doStuff(); // invoke the static method
}
}
Running this code produces the output:
a a a
Some case:
in the subclass Dog, if the method dostuff() is not
static,it will be compile time error in the above code
block .
So a static method cannot be overriden
Is This Answer Correct ? | 2 Yes | 0 No |
Post New Answer View All Answers
What is the difference between the session.get() method and the session.load() method?
How are the elements of a borderlayout organized?
Why do I get a duplicate name error when loading a jar file?
What if the static modifier is removed from the signature of the main method?
Do we need to override service() method
What are the benefits of detached objects?
How database connectivity in XML is achieved?
What is local interface. How values will be passed?
what is handle?
Why are some of the class and element names counter-intuitive?
which type of objects reference will be given to client?
When is the best time to validate input?
What is the relation between the infobus and rmi?
Explain what is synchronization?
Explain the difference between object state and behavior?