Can we override static methods?
Answer Posted / neha jain
dear yogesh
sory for wrong answer we can not override static method
with static method that known as method hiding.plz view
following example
Briefly, when you override a method, you still get the
benefits of run-time polymorphism, and when you hide, you
don't. So what does that mean? Take a look at this code:
class Foo {
public static void classMethod() {
System.out.println("classMethod() in Foo");
}
public void instanceMethod() {
System.out.println("instanceMethod() in Foo");
}
}
class Bar extends Foo {
public static void classMethod() {
System.out.println("classMethod() in Bar");
}
public void instanceMethod() {
System.out.println("instanceMethod() in Bar");
}
}
class Test {
public static void main(String[] args) {
Foo f = new Bar();
f.instanceMethod();
f.classMethod();
}
}
If you run this, the output is
instanceMethod() in Bar
classMethod() in Foo
| Is This Answer Correct ? | 7 Yes | 0 No |
Post New Answer View All Answers
Can you start a thread twice in Java?
What modifiers may be used with a top-level class?
What are the 6 functions?
What is meant by data hiding/encapsulation?
What is a website container?
Can we have more than one package statement in source file ?
Is null keyword in java?
Explain, java is compatible with all servers but not all browsers?
What is hashtable and explain features of hashtable?
What are scriptlets?
Define nashorn in java8.
How we can declare a static variable?
What is the purpose of the finalize() method?
what is meant by encapsulation?
Name few java util classes introduced with java 8 ?