What is method hiding in Java? Even the JavaDoc explanation is confusing

前端 未结 7 1287
没有蜡笔的小新
没有蜡笔的小新 2020-11-28 02:36

Javadoc says:

the version of the hidden method that gets invoked is the one in the superclass, and the version of the overridden method that gets invo

7条回答
  •  执念已碎
    2020-11-28 02:46

    For example you can override instance methods in a super class but not static.

    Hiding is Parent class has a static method named Foo and the sub class also has a static method called Foo.

    Another scenario is the parent has a static method named Cat and the sub class has an instance method named Cat. (static and instance with the same signature can't intermix).

    public class Animal {
    
      public static String getCat() { return "Cat"; }
    
      public boolean isAnimal() { return true; }
    }
    
    public class Dog extends Animal {
    
      // Method hiding
      public static String getCat() { }
    
      // Not method hiding
      @Override
      public boolean isAnimal() { return false; }
    }
    

提交回复
热议问题