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
Does sprintf add a null terminator?
List the interfaces which extends collection interface?
What are the types of exceptions?
What will happen if non-synchronized method calls a static synchronized method and what kind of lock it acquires?
What is the difference between a checked and an unchecked exception?
What are computer functions?
What is the difference between the jdk 1.02 event model and the event-delegation model introduced with jdk 1.1?
What is rule of accessibility in java?
What is the use of hashmap in java?
What is constructor chaining in java?
Which programming language is best in future?
What is a local, member and a class variable?
What is binary tree in java?
Suppose if we have variable ' I ' in run method, if I can create one or more thread each thread will occupy a separate copy or same variable will be shared?
What are synchronized methods and synchronized statements in java programming?