Overriding vs Hiding Java - Confused
Solution 1
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.
Solution 2
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
Solution 3
This is the difference between overrides and hiding,
- If both method in parent class and child class are an instance method, it called overrides.
- If both method in parent class and child class are static method, it called hiding.
- One method cant be static in parent and as an instance in the child. and visa versa.
Solution 4
If I understand your question properly then the answer is "you already are overriding".
"Which shows me that writing a static method, with the same name as in the parent, in a subclass pretty much does an override."
If you write a method in a subclass with exactly the same name as a method in a superclass it will override the superclass's method. The @Override annotation is not required to override a method. It does however make your code more readable and forces the compiler to check that you are actually overriding a method (and didn't misspell the subclass method for example).
Solution 5
Overriding happens only with instance methods. When the type of the reference variable is Animal and the object is Cat then the instance method is called from Cat (this is overriding). For the same acat object the class method of Animal is used.
public static void main(String[] args) {
Animal acat = new Cat();
acat.testInstanceMethod();
acat.testClassMethod();
}
Output is:
The instance method in Cat.
Class method in Animal.
Lostlinkpr
Updated on July 21, 2022Comments
-
Lostlinkpr almost 2 years
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 theAnimal
class executes the method inAnimal
class is pretty obvious, nothing special there. Then they call thetestInstanceMethod()
from a reference tomyCat
, so again pretty obvious that the method executed then is the one in the instance ofCat
.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!
-
wolfcastle about 12 yearsYou cut and pasted that table directly from the tutorial the OP said didn't help him understand.
-
Paul Bellora about 12 yearsThis answer fails to address instance vs static methods with regard to overriding/hiding.
-
Lostlinkpr about 12 yearsThank you for the quick answer, this clarifies it! I noticed that in the JavaRanch example, they used the variable to call the class method instead of using the class directly which makes it easier to understand. I guess in the Java tutorial they used the class directly because using an instance to call a static method is probably not good practice, but they should have used myAnimal.testClassMethod() instead of Animal.testClassMethod().
-
WhyNotHugo about 12 years+1 for being able to put it down in words properly, rather than by example! :)
-
tutak almost 11 yearsThe table makes it very clear, in the examples not all cases were considered.
-
gstackoverflow about 10 years@Kazekage Gaara Is there difference between overloading and hiding ?
-
Paschalis almost 10 yearsI agree of course with the answer, but how about
private methods
? They can't beoverridden
since the subclass does not know about their existence.. Therefore they might behidden
instead. -
Shubham Mittal almost 8 yearsExcellent Answer ! Though you might add the example at coderanch for the sake of completeness :)
-
Ritesh Puri about 7 years@Kazekage Gaara - I understood your answer but I have a doubt. I have read it online that 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. Like in above case we have "public static void testClassMethod()" defined in both classes and when "Animal.testClassMethod();" is executed, "testClassMethod" method in the parent class is getting called and hence, the child class method has got hidden. Can you please help me in understanding this point?
-
amarnath harish almost 6 yearsok, then what would happen if i call ` dog husky = new dog();' and call
husky.Animal();
will it print animal.something or dog.something? i guess its WRONG to say ** that **This will always call Animal.something() -
amarnath harish almost 6 yearswonder why it didnt get the upvotes..much appreciated answer.
-
Sri9911 about 5 yearsThis is not an answer. It's extension of the question asked. It may have been a separate question itself or a part of comments on the question.
-
scorpion almost 4 yearsI'm trying to understand what the word hiding itself means. What is hiding what? Is the static method in the Parent class hidden because (least expected) it gets invoked? Or is the static method in the Child class hidden, because it doesn't gets invoked?
-
Dude156 over 3 years@amarnathharish You can't do
.Animal()
, rememberAnimal()
is a constructor. -
Dude156 over 3 yearsAnd as further clarification for anyone wondering, the reason the
something()
inAnimal()
always call's Animal'ssomething()
is because a call to a static method is resolved in compile time rather than run-time. This means that the static method call inAnimal()
is always implicitly callingAnimal.something()
. This is pretty intuitive if you think about it: a call to a static method must be preceded by a class name (i.e.className.staticMethodName()
) unless the call is in the same class.