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

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 invoked is the one in the subclass.

doesn't ring a bell to me. Any clear example showing the meaning of this will be highly appreciated.


public class Animal {
    public static void foo() {
        System.out.println("Animal");
    }
}

public class Cat extends Animal {
    public static void foo() {  // hides Animal.foo()
        System.out.println("Cat");
    }
}

Here, Cat.foo() is said to hide Animal.foo() . Hiding does not work like overriding, because static methods are not polymorphic. So the following will happen:

Animal.foo(); // prints Animal
Cat.foo(); // prints Cat

Animal a = new Animal();
Animal b = new Cat();
Cat c = new Cat();
Animal d = null;

a.foo(); // should not be done. Prints Animal
b.foo(); // should not be done. Prints Animal because the declared type of b is Animal
c.foo(); // should not be done. Prints Cat because the declared type of c is Cat
d.foo(); // should not be done. Prints Animal because the declared type of b is Animal

Calling static methods on instances rather than classes is a very bad practice, and should never be done.

Compare this with instance methods, which are polymorphic and are thus overridden. The method called depends on the concrete, runtime type of the object:

public class Animal {
    public void foo() {
        System.out.println("Animal");
    }
}

public class Cat extends Animal {
    public void foo() { // overrides Animal.foo()
        System.out.println("Cat");
    }
}

Then the following will happen:

Animal a = new Animal();
Animal b = new Cat();
Animal c = new Cat();
Animal d = null;

a.foo(); // prints Animal
b.foo(); // prints Cat
c.foo(); // prints Cat
d.foo(): // throws NullPointerException

First of all What is meant by method Hiding?

Method hiding means subclass has defined a class method with the same signature as a class method in the superclass. In that case the method of superclass is hidden by the subclass. It signifies that : The version of a method that is executed will NOT be determined by the object that is used to invoke it . In fact it will be determined by the type of reference variable used to invoke the method.

What is meant by method overriding?

Method overriding means subclass had defined an instance method with the same signature and return type( including covariant type) as the instance method in superclass. In that case method of superclass is overridden(replaced) by the subclass. It signifies that: The version of method that is executed will be determined by the object that is used to invoke it . It will not be determined by the type of reference variable used to invoke the method.

Why can't static methods be overridden?

Because, static methods are resolved statically (ie at compile time) based on the class they are called on and not dynamically as in the case with instance methods which are resolved polymorphically based on the runtime type of the object.

How should static methods be accessed?

Static methods should be accessed in static way. ie by the name of class itself rather than using an instance.

Here is the short Demo for method overriding and hiding:

class Super
{
  public static void foo(){System.out.println("I am foo in Super");}
  public void bar(){System.out.println("I am bar in Super");}
}
class Child extends Super
{
  public static void foo(){System.out.println("I am foo in Child");}//Hiding
  public void bar(){System.out.println("I am bar in Child");}//Overriding
  public static void main(String[] args)
  {
     Super sup = new Child();//Child object is reference by the variable of type Super
     Child child = new Child();//Child object is referenced by the variable of type Child
     sup.foo();//It will call the method of Super.
     child.foo();//It will call the method of Child.

     sup.bar();//It will call the method of Child.
     child.bar();//It will call the method of Child again.
  }
}

Output is

I am foo in Super
I am foo in Child
I am bar in Child
I am bar in Child

Clearly, as specified, since foo is the class method so the version of foo invoked will be determined by the type of reference variable (ie Super or Child) referencing the object of Child . If it is referenced by Super variable then foo of Super is called. And if it is referenced by Child variable then foo of Child is called.
Whereas,
Since bar is the instance method so the version of bar invoked is solely determined by the object(ie Child ) that is used to invoke it. No matter via which reference variable ( Super or Child ) it is called , the method which is going to be called is always of Child .


To overwrite a method means that whenever the method is called on an object of the derived class, the new implementation will be called.

To hide a method means that an unqualified call to that name in the scope of this class (ie in the body of any of its methods, or when qualified with the name of this class) will now call a completely different function, requiring a qualification to access the static method of the same name from the parent class.

More description Java Inheritance: Overwritten or hidden methods

链接地址: http://www.djcxy.com/p/13822.html

上一篇: 为什么Java不允许重写静态方法?

下一篇: 什么是隐藏在Java中的方法? 即使是JavaDoc的解释也是令人困惑的