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 private method static?
What do you mean by ternary operator in java?
What is a copy constructor in java?
How to split arraylist elements in java?
What is the requirement of thread in java?
What is the use of flag?
How many tetrahedral voids are there in bcc?
How would overload a function based on return type?
When should you use arraylist and when should you use linkedlist?
How do you trim a space in java?
What language is an assembler written in?
Is set sorted in java?
Does java map allow duplicates?
What is arrays aslist in java?
what is comparable and comparator interface?