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


Please Help Members By Posting Answers For Below Questions

What is the difference between the session.get() method and the session.load() method?

610


How are the elements of a borderlayout organized?

612


Why do I get a duplicate name error when loading a jar file?

552


What if the static modifier is removed from the signature of the main method?

717


Do we need to override service() method

1152






What are the benefits of detached objects?

597


How database connectivity in XML is achieved?

1775


What is local interface. How values will be passed?

563


what is handle?

1867


Why are some of the class and element names counter-intuitive?

581


which type of objects reference will be given to client?

2040


When is the best time to validate input?

616


What is the relation between the infobus and rmi?

567


Explain what is synchronization?

587


Explain the difference between object state and behavior?

582