As per the rule, while overriding a method in subclass, parameters cannot be changed and have to be the same as in the super class. What if we pass subclass of parameter while overriding method ? Will it be called as overloading or overriding?
Based on my query I have written some code below.
I was expecting the output as "Dog eats Flesh Food" but to my surprise the output is "Animal eats Flesh Food"
Will appreciate if someone can explain how does Animal method gets called when the object assigned is of type Dog ?
class Food {
public String toString(){
return "Normal Food";
}
}
class Flesh extends Food {
public String toString(){
return "Flesh Food";
}
}
class Animal {
public void eat(Food food){
System.out.println("Animal eats "+ food);
}
}
class Dog extends Animal{
public void eat(Flesh flesh){
System.out.println("Dog eats "+ flesh);
}
}
public class MyUtil {
public static void main(String[] args) {
Animal animal = new Dog();
Flesh flesh = new Flesh();
animal.eat(flesh);
}
}
Overriding Super Class Methods in Java | Inheritance in Java 9.3 Overriding Superclass Methods In a class hierarchy, when a method in a subclass has the same name and type signature as a method in its superclass, then the method in the subclass is said to override the method in the superclass.
As per the rule, while overriding a method in subclass, parameters cannot be changed and have to be the same as in the super class. What if we pass subclass of parameter while overriding method ? Will it be called as overloading or overriding? Based on my query I have written some code below.
In a class hierarchy, when a method in a subclass has the same name and type signature as a method in its superclass, then the method in the subclass is said to override the method in the superclass.
Super keyword in Method Overriding The super keyword is used for calling the parent class method/constructor. super.myMethod () calls the myMethod () method of base class while super () calls the constructor of base class. Let’s see the use of super in method Overriding.
The method eat
in Dog
does not override the method eat
in Animal
. This is because the arguments are different (one requires Flesh
, the other requires Food
).
The eat
methods are overloads.
Choosing between overloads takes place at compile time, not runtime. It is not based on the actual class of the object on which the method is invoked, but the compile-time type (how the variable is declared).
animal
has compile-time type Animal
. We know this because the declaration of the variable animal
was Animal animal = ...
. The fact that it is actually a Dog
is irrelevant - it is the version of eat
in Animal
that must be invoked.
On the other hand, the toString
method in Flesh
does override the toString
method in Food
.
When one method overrides another it is the actual class of the object that the method is invoked on that determines which version runs.
In the eat
method of Animal
, even though the argument has compile-time type Food
, if you pass an instance of Flesh
to it, it is the toString
method in Flesh
that will execute.
Therefore you get the message "Animal eats Flesh Food"
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With