Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In java, Can we override a method by passing subclass of the parameter used in super class method?

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);
        }
    }
like image 880
SD Shaw Avatar asked Aug 04 '15 23:08

SD Shaw


People also ask

What is overriding superclass methods in Java?

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.

What if we pass subclass of parameter while overriding method?

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.

What is the difference between superclass and subclass in Java?

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.

How do you use Super in a method override?

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.


1 Answers

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".

like image 181
Paul Boddington Avatar answered Oct 12 '22 16:10

Paul Boddington