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
What is a parameter used for?
What are static initalizers in java ?
What's the default access specifier for variables and methods of a class?
How thread scheduler schedule the task?
What is difference between static and abstract class?
Is space a character in java?
What is a bufferedreader?
What is a nullable field?
What are some examples of variable costs?
What is mnemonic code?
Is java util list serializable?
What is the meaning of variable in research?
What is a default constructor and also define copy contrucyor?
What does sizeof return?
Can a static block throw exception?