In this post you will learn one of the famous interview question for fresher and Experienced Java Developers that is Can we override Static methods in Java. No static methods cannot be overridden, if subclass tries to it will be called Method hiding and not overriding because there won't be any runtime polymorphism and decision as to which method to invoked is decided at compile time.(if any call is decided at compile time then there is no concept of overriding.)
Lets understand with example:
public class MainClass {
public static void main(String[] args){
Parent.print();
Child.print();
}
}
class Parent {
public static void print() {
System.out.println("I am Parent");
}
}
class Child extends Parent {
}
Output:
I am Parent
I am Parent
Explanation:
Static methods including instance methods(public, protected and default) are inherited to subclass.
Only difference is, if subclass defines method with same name and signature that is present in super class then there is difference in the way static method and instance method will work.
STATIC METHODS:
If subclass defines method with same name & signature as one present in Superclass then for STATIC METHODS it is said to be method hiding and not method overriding.
It is said to be method hiding because there will be no polymorphic behavior achieved.
Lets understand with example
public class MainClass {
public static void main(String[] args){
Parent parent = new Child();
parent.print();
Child child = new Child();
child.print();
}
}
class Parent {
public static void print() {
System.out.println("I am Parent");
}
}
class Child extends Parent {
public static void print() {
System.out.println("I am Child");
}
}
Output of above program is
I am Parent,
I am Child,
because no polymorphism is achieved and instance type that is,
for line parent.print(); instance type is Parent and print() method of Parent class will be invoked.
for line child.print(); instance type is Child and print() method of Child class will be invoked.
Note: If it exhibits polymorphism then output will be "I am Child" for line parent.print().
METHOD HIDING
Child class also provides implementation of print method. so child.print(); method invokes print() method of Child class and not Parent class because Child class has hide the super class print() method by providing implementation of it and that is why it is called Method hiding.
INSTANCE METHODS:
If subclass defines method with same name & signature as one present in Superclass then for INSTANCE METHODS it is said to be method overriding and not method hiding.
It is said to be method overriding because there will be polymorphic effect.
If we run the same above program by removing static to print() method and making it instance method then output will be .
I am Child.
I am Child.
output of line parent.print(); is "I am Child" because it checks whether Parent class has print() method and yes it has then it checks whether Subclass has provided his own implementation of print method, Yes, so print() method of Child class is invoked.
No comments:
Post a Comment