According to the documentation
An instance method in a subclass with the same signature (name, plus the number and the type of its parameters) and return type as an instance method in the superclass overrides the superclass's method.
While in case of static methods
If a subclass defines a static method with the same signature as a static method in the superclass, then the method in the subclass hides the one in the superclass.
So, I have tested the code example shown there with addition of more use cases:
The super class Animal
:
public class Animal {
public static void testClassMethod() {
System.out.println("The static method in Animal");
}
public void testInstanceMethod() {
System.out.println("The instance method in Animal");
}
}
The subclass Cat
:
public class Cat extends Animal {
public static void testClassMethod() {
System.out.println("The static method in Cat");
}
public void testInstanceMethod() {
System.out.println("The instance method in Cat");
}
public static void main(String[] args) {
Cat myCat = new Cat();
Animal myAnimal = myCat;
Animal animal = new Animal();
Animal.testClassMethod();
Cat.testClassMethod();
animal.testInstanceMethod();
myAnimal.testInstanceMethod();
}
}
The output here is:
The static method in Animal
The static method in Cat
The instance method in Animal
The instance method in Cat
So, I still see no actual difference between overriding the superclass instance method with subclass instance method with the same signature and overriding (hiding) the superclass static (class) method with subclass static method with the same signature.
What am I missing here?