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 initial size of arraylist in java?
Describe 2 different ways to concatenate two strings.
Explain restrictions for using anonymous inner classes?
How a string is stored in memory?
What are the different tags provided in jstl?
What is passed by reference and pass by value ?
Can we clone singleton class in java?
What is the basic of java?
Compare Mutex and Semaphore in java.
What does nullpointerexception mean?
How do you escape json?
What things should be kept in mind while creating your own exceptions in java?
what methods would you overwrite in java.lang.object class?
What is meant by interface?
How does arraylist work in java?