From the docs, "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."
I understand the difference between method hiding and overriding. However, it's strange to say that the subclass hides the superclass method because if you have the following:
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.testClassMethod();
myAnimal.testInstanceMethod();
}
}
The superclass's static method is called. But by the definition of hiding, the method in the subclass is hiding the one in the superclass. I don't see how the subclass is "covering up/hiding" the superclass static method, as the superclass's method is the one that's actually called.
Cat
'stestClassMethod()
hidesAnimal.testClassMethod()
, since if you didn't have a statictestClassMethod()
in theCat
class,Cat.testClassMethod()
would invokeAnimal.testClassMethod()
.When you call
Animal.testClassMethod()
, it's can't be hidden by a sub-class's method.Yes. But that is because you explicitly named the superclass's static method by qualifying with the superclass name in the call statement.
If you had written the
main
like this instead:then you would have seen that the
Cat
version oftestClassMethod
was called. Here, theCat.testClassMethod
method hides theAnimal.testClassMethod
methodI think it's called "hiding" because you can no longer access the superclass's method simply by writing the method name. Without the
public static void testClassMethod()
definition inCat
, yourmain
could sayto call the method defined in
Animal
. But you can no longer do that. Yes, you can still call it by giving it the qualified name:so it's not completely hidden. But please note that people who write language standards have to come up with names for some concepts that may not quite match the meanings we give words in the non-computing world, so sometimes coming close is the best they can do. You can't try to take the terminology literally. The term
hidden
has a formal definition somewhere in the JLS, I believe; and that means when you see the term used in the JLS, it means whatever the JLS defines it to mean, neither more nor less.Consider adding this to your posted example
Now if you do this:
each of the above will give different output. So you could say that the static method in
Cat
did hide theAnimal
's static method - while there's no such thing as overriding for static methods, one of the animals printed according toAnimal
's code and the other did not.(P.S. I'm definitely not encouraging you to call static methods in this way.)