Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overriding vs Hiding Java - Confused

I'm confused on how overriding differs from hiding in Java. Can anyone provide more details on how these differ? I read the Java Tutorial but the sample code still left me confused.

To be more clear, I understand overriding well. My issue is that I don't see how hiding is any different, except for the fact that one is at the instance level while the other is at the class level.

Looking at the Java tutorial code:

public class Animal {     public static void testClassMethod() {         System.out.println("Class" + " method in Animal.");     }     public void testInstanceMethod() {         System.out.println("Instance " + " method in Animal.");     } } 

Then we have a subclass Cat:

public class Cat extends Animal {     public static void testClassMethod() {         System.out.println("The class method" + " in Cat.");     }     public void testInstanceMethod() {         System.out.println("The instance method" + " in Cat.");     }      public static void main(String[] args) {         Cat myCat = new Cat();         Animal myAnimal = myCat;         Animal.testClassMethod();         myAnimal.testInstanceMethod();     } } 

Then they say:

The output from this program is as follows:

Class method in Animal.

The instance method in Cat.

To me, the fact that calling a class method testClassMethod() directly from the Animal class executes the method in Animal class is pretty obvious, nothing special there. Then they call the testInstanceMethod() from a reference to myCat, so again pretty obvious that the method executed then is the one in the instance of Cat.

From what I see, the call hiding behaves just like overriding, so why make that distinction? If I run this code using the classes above:

Cat.testClassMethod(); 

I'll get: The class method in Cat. But if I remove the testClassMethod() from Cat, then I'll get: The class method in Animal.

Which shows me that writing a static method, with the same signature as in the parent, in a subclass pretty much does an override.

Hopefully I'm making clear my where I'm confused and someone can shed some light. Thanks very much in advance!

like image 968
Lostlinkpr Avatar asked May 15 '12 04:05

Lostlinkpr


People also ask

What is the difference between overriding and hiding in Java?

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.

What is the difference between hiding and overriding?

Hiding redefines the complete method, whereas overriding redefines only the implementation of the method. In Overriding, you can access the base class using the child class' object overridden method.. Shadowing has cannot access the child class methods.

Why do we use 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.

What is the difference between method hiding and method overloading?

For Method overriding override keyword is being used. In case of Method Hiding new keyword is used to define new implementation in child class. In Method Overriding the implementation type of the method is of object type. However on other hand implementation type of method in Method hiding is of reference type.


2 Answers

Overriding basically supports late binding. Therefore, it's decided at run time which method will be called. It is for non-static methods.

Hiding is for all other members (static methods, instance members, static members). It is based on the early binding. More clearly, the method or member to be called or used is decided during compile time.

In your example, the first call, Animal.testClassMethod() is a call to a static method, hence it is pretty sure which method is going to be called.

In the second call, myAnimal.testInstanceMethod(), you call a non-static method. This is what you call run-time polymorphism. It is not decided until run time which method is to be called.

For further clarification, read Overriding Vs Hiding.

like image 107
Kazekage Gaara Avatar answered Oct 07 '22 00:10

Kazekage Gaara


Static methods are hidden, non-static methods are overriden. The difference is notable when calls are not qualified "something()" vs "this.something()".

I can't really seem to put it down on words, so here goes an example:

public class Animal {      public static void something() {         System.out.println("animal.something");     }      public void eat() {         System.out.println("animal.eat");     }      public Animal() {         // This will always call Animal.something(), since it can't be overriden, because it is static.         something();         // This will call the eat() defined in overriding classes.         eat();     }  }   public class Dog extends Animal {      public static void something() {         // This method merely hides Animal.something(), making it uncallable, but does not override it, or alter calls to it in any way.         System.out.println("dog.something");     }      public void eat() {         // This method overrides eat(), and will affect calls to eat()         System.out.println("dog.eat");     }      public Dog() {         super();     }      public static void main(String[] args) {         new Dog();     }  } 

OUTPUT:

animal.something dog.eat 
like image 20
WhyNotHugo Avatar answered Oct 07 '22 01:10

WhyNotHugo