public class Main {
public static void main(String[] args) {
ParentClass pc = new ChildClass();
pc.display();
}
}
public class ParentClass{
public static void display(){
System.out.println("hello from parent static-display");
}
public void show(){
System.out.println("hello from parent non-static-show");
}
}
public class ChildClass extends ParentClass{
public static void display(){
System.out.println("hello from child static-display");
}
public void show(){
System.out.println("hello from child non-static-show");
}
}
Output:
hello from parent static-display
Explanation:
Here, we are creating an object for the parent class and referring to the child class which both has static method, since we cannot create object for static method, we are now creating an object and then pointing to child, so the parent class will hide the static method in the child class(method hiding).
Fun way to remember:
point no:1 creating an object for static method is wrong
point no:2 Okay, you created an object and referring to the child
ParentClass pc = new ChildClass();
pc.display();
point no:3 Parent class will say, I should not have let you create the object in the first place yet, I allowed you to create but now you are going to kill(override)me. I can't bear it, I will block you(method hiding)