Why is it called the "hiding method"?

From docs : "If a subclass defines a static method with the same signature as the static method in the superclass, the method in the subclass hides one in the superclass."

I understand the difference between hiding and overriding a method. However, it is 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 static method of the superclass is called. But by definition of concealment, a method in a subclass hides one in a superclass. I don’t see how the subclass “hides / hides” the static method of the superclass, since the superclass method is the one that is actually called.

+4
4

.

. , , .

main :

public static void main(String[] args) {
    ...
    testClassMethod();
}

, Cat testClassMethod. Cat.testClassMethod Animal.testClassMethod

+7

Cat testClassMethod() Animal.testClassMethod(), , testClassMethod() Cat, Cat.testClassMethod() Animal.testClassMethod().

Animal.testClassMethod(), .

+2

,

class Dog extends Animal {
    // nothing here
}

, :

new Cat().testClassMethod();
new Dog().testClassMethod();

. , , Cat Animal, , , , Animal, - .

(P.S. .)

0

, "", , . public static void testClassMethod() Cat main

testClassMethod();

, Animal. . , , :

Animal.testClassMethod();

. , , , , , , , , . . , hidden - JLS; , , JLS, , JLS , , .

0

Source: https://habr.com/ru/post/1615530/


All Articles