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 make a constructor final?
What is the importance of finally block in exception handling?
What are the different types of collections in java?
What is java abstraction with example?
What is the difference between array and array list in java?
Is it necessary for the port addresses to be unique? Explain with reason.
What is a nonetype?
Do I need java on my pc?
Why java is secure? Explain.
What is a double?
How do I convert a string to an int in java?
How do you insert a line break?
What is the use of list in java?
How do you write a scanner class in java?
What do you understand by soft reference?