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

自古美人都是妖i 提交于 2019-11-26 07:57:34

问题


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.


回答1:


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 because the declared type of a is 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 d 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



回答2:


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 (i.e. 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. i.e. 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 (i.e 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(i.e 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.




回答3:


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 (i.e. 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




回答4:


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");
  }
}



回答5:


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; }
}



回答6:


 class P
    {
    public static  void m1()
    {
    System.out.println("Parent");
    }
}
    class C extends P
    {
    public static void m1()
    {
    System.out.println("Child");
    }
}
class Test{
    public static void main(String args[])
    {
    Parent p=new Parent();//Parent
    Child c=new Child();  //Child
    Parent p=new Child();  //Parent
    }
    }

If the both parent and child class method are static the compiler is responsible for method resolution based on reference type

class Parent
{
public void m1()
{
System.out.println("Parent");
}}
class Child extends Parent
{
public void m1()
{
System.out.println("Child")
}
}
class Test
{
public static void main(String args[])
{
Parent p=new Parent(); //Parent 
Child c=new Child();   //Child
Parent p=new Child();  //Child
}
}

If both method are not static  jvm is responsible for method resolution based on run time object


来源:https://stackoverflow.com/questions/16313649/what-is-method-hiding-in-java-even-the-javadoc-explanation-is-confusing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!