Can we override static methods?
Answer Posted / sadheez
It may seems to be overriding the static methods, but the
real fact is HIDING.
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 StaticHiding {
public static void main(String[] args) {
Foo f = new Bar();
f.instanceMethod();
f.classMethod();
when u run this program output will be:
instanceMethod() in Bar
classMethod() in Foo.
Here if u say it to be overriding then the
subclass ie., Bar class having static classMethod() should
be executed. But the fact here is Foo class static
classMethod() is executed.
So its method HIDING and not method
overriding..
I hope i have given answer to my best if
anyone feels wrong plz do post ur suggestions..
| Is This Answer Correct ? | 59 Yes | 1 No |
Post New Answer View All Answers
What do you mean by synchronized non access modifier?
Is set sorted in java?
What are "methods" and "fields"?
What do you mean by stack?
How to declare objects of a class ?
How does queue work in java?
What is meant by class and object in java?
When should we create our own custom exception classes?
What is a short in java?
Can an abstract class be a final class?
What is the use of put method?
What is difference between identifier and variable?
When will we prefer to use set and list in java and why?
Why parsing is done?
Explain which of the following methods releases the lock when yield(), join(),sleep(),wait(),notify(), notifyall() methods are executed?