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

前端 未结 7 1264
没有蜡笔的小新
没有蜡笔的小新 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 03:01

    If a subclass defines a class method with the same signature as a class method in the superclass, the method in the subclass hides the one in the superclass.

    Hidden methods are in Static context, I believe. Static methods are not overridden, per se, because the resolution of method calls done by the compiler at the compile time itself. So, if you define a static method in the base class with the same signature as that one present in the parent class, then the method in the subclass hides the method inherited from super class.

    class Foo {
      public static void method() {
         System.out.println("in Foo");
      }
    }
    
    class Bar extends Foo {
       public static void method() {
        System.out.println("in Bar");
      }
    }
    
    0 讨论(0)
提交回复
热议问题