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
How we can skip finally block of exception even if some exception occurs in the exception block in java?
What is the program development process?
How does arrays sort work in java?
Can size_t be negative?
What is default constructors?
What is downcasting?
How many bytes is 255 characters?
What is string in java is it a data type?
What is assembly used for?
What is a numeric digit?
Why do we override tostring method in java?
What is t in generics in java?
How many types of the indexof method are there for strings?
Explain covariant method overriding in java.
Is java a prime method?