Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Polymorphism in java: Why do we set parent reference to child object?

I want to understand the use-case of setting a parent reference to a child object. Example: Dog class extends Animal class. (No interfaces, mind it) I would normally create an object of Dog like this:

Dog obj = new Dog();

Now, since Dog is a subclass of Animal it already has access to all of Animal's methods and variables. Then, what difference does this make:

Animal obj = new Dog(); 

Please provide a proper use-case with an code snippet of its use. No theoretical articles about 'Polymorphism' or 'Coding to interfaces' please!

Code:

public class Polymorphism {
    public static void main(String[] args){
        Animal obj1 = new Dog();
        Dog obj2 = new Dog();
        obj1.shout(); //output is bark..
        obj2.shout(); //output is bark..        
    }   
}

class Animal{
    public void shout(){
        System.out.println("Parent animal's shout");
    }       
}

class Dog extends Animal{
    public void shout(){
        System.out.println("bark..");
    }
}

class Lion extends Animal{
    public void shout(){
        System.out.println("roar..");
    }
}

class Horse extends Animal{
    public void shout(){
        System.out.println("neigh");
    }
}

Output is the same for both the cases. Then why do we set parent reference to child object?

like image 296
Subhadeep Banerjee Avatar asked Aug 21 '15 13:08

Subhadeep Banerjee


People also ask

Why do we assign a parent reference to the child object in Java?

The parent class can hold reference to both the parent and child objects. If a parent class variable holds reference of the child class, and the value is present in both the classes, in general, the reference belongs to the parent class variable. This is due to the run-time polymorphism characteristic in Java.

Can we assign parent object to child objects?

We can assign child class object to parent class reference variable but we can't assign parent class object to child class reference variable. Save this answer.

Can we assign child reference to parent object in Java?

In Java, the reference variable of the Parent class is capable to hold its object reference as well as its child object reference.

What is the use of polymorphic reference in Java?

A polymorphic reference is a variable that can refer to different types of objects at different points in time. It is typically compatible with the class that it refers to.


1 Answers

This is an implementation of a principle which says -

Program to an interface, not to an implementation.

As an example, if you design a method to accept a reference of type Animal, then in future you can easily pass an= Cat implementation to it (provided of course that the Cat is a sub-type of Animal.

Which means -

public void doSomethingWithAnimal(Animal animal) {
    // perform some action with/on animal
}

is much more flexible than -

public void doSomethingWithAnimal(Dog d) {
    // your code
}

because for the first method, you can easily do something like -

doSomethingWithAnimal(new Cat());

if you ever decide to create new Cat type, inheriting from Animal.

like image 68
MD Sayem Ahmed Avatar answered Nov 15 '22 20:11

MD Sayem Ahmed