Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 843
JATMON Avatar asked May 01 '13 06:05

JATMON


People also ask

What is method hiding in Java?

Method hiding can be defined as, "if a subclass defines a static method with the same signature as a static method in the super class, in such a case, the method in the subclass hides the one in the superclass." The mechanism is known as method hiding. It happens because static methods are resolved at compile time.

Why it is called method hiding in Java?

When super class and sub class contains same method including parameters and if they are static. The method in the super class will be hidden by the one that is in the sub class. This mechanism is known as method hiding.

Why do we use method hiding?

It tells us to use the new keyword to hide the inherited member. So, by using the new modifier in the derived class method, it hides the implementation of the base class method. This is called Method Hiding. It allows you to provide a new implementation for a derived class.

What is difference between method overriding and method hiding?

In method overriding, when base class reference variable pointing to the object of the derived class, then it will call the overridden method in the derived class. In the method hiding, when base class reference variable pointing to the object of the derived class, then it will call the hidden method in the base class.


2 Answers

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(); Cat c = new Cat(); Animal d = null;  a.foo(); // prints Animal b.foo(); // prints Cat c.foo(); // prints Cat d.foo(): // throws NullPointerException 
like image 53
JB Nizet Avatar answered Sep 20 '22 06:09

JB Nizet


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.

like image 32
Vishal K Avatar answered Sep 24 '22 06:09

Vishal K