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.
.
. , , .
main :
main
public static void main(String[] args) { ... testClassMethod(); }
, Cat testClassMethod. Cat.testClassMethod Animal.testClassMethod
Cat
testClassMethod
Cat.testClassMethod
Animal.testClassMethod
Cat testClassMethod() Animal.testClassMethod(), , testClassMethod() Cat, Cat.testClassMethod() Animal.testClassMethod().
testClassMethod()
Animal.testClassMethod()
Cat.testClassMethod()
Animal.testClassMethod(), .
,
class Dog extends Animal { // nothing here }
, :
new Cat().testClassMethod(); new Dog().testClassMethod();
. , , Cat Animal, , , , Animal, - .
Animal
(P.S. .)
, "", , . public static void testClassMethod() Cat main
public static void testClassMethod()
testClassMethod();
, Animal. . , , :
Animal.testClassMethod();
. , , , , , , , , . . , hidden - JLS; , , JLS, , JLS , , .
hidden
Source: https://habr.com/ru/post/1615530/More articles:Plugin maven berth without console logs when starting forked - loggingFlexbox Scrolling Does Not Work in Firefox - htmlLaravel Timestamp Format / JavaScript Date - javascriptConvert UNIX timestamp to date (javascript) - javascriptGit Ошибка: сохранение или фиксация - gitSymfony ManyToMany Form - phphow to submit form values in 2 different symfony tables - phpDictionary - StringComparison.CurrentCultureIgnoreCase - c #Overwriting URIs on nginx does not work when using variables - nginxResponsive rectangle with maximum image size while maintaining aspect ratio - htmlAll Articles