简体   繁体   中英

Dynamic binding in Java. What are benefits and drawbacks of using Parent as a type and Child as an object?

I've read a couple of articles about the static and dynamic binding techniques in Java.

My question is what is the practical difference between

Parent a = new Parent();

and

Parent a = new Child();

?

If you don't want Child object, then second approach doesn't make sense.

Second approach make sense only in case where you want child object but the flexibility to replace Child object with Child2 or Some other Child Object which extends Parent object transparently at later point of time.

One of the very frequently used example case is List , ArrayList , LinkedList . If you want to return List object type but have a flexibility to replace ArrayList with LinkedList (or) LinkedList with ArrayList in your implementation, then you would write code like:

List someRefer = new ArrayList();

At some point, if your implementation requires lot of inserts/deletes to List, then you realized that LinkedList is best data structure for this case, then you have flexibility to simply change your code to:

List someRefer = new LinkedList();

In the second case where you assign a child object to a parent object, you are allowing the child to act as a parent so that it can interact with other parent objects. In reality, it's still a child with its own fields and methods (overridden or not), but now you can use it like it were a parent object. Put it in an array of parents, compare it to another parent, etc.

The situation created is also known as runtime polymorphism which means the Jvm will decide whose class members to access at runtime.For ex

class Parent {
    public void disp() {
        System.out.println("Parent");
    }
}
public class Child extends Parent {
    public void disp() {
        System.out.println("Child");
    }
    public static void main(String args[]) {
        Parent p = new Parent();
        Parent p1 = new Child();
        p.disp();

        p1.disp();

    }
}

output

Parent
Child

Means the jvm calls the method on the specified object type.When you used object type to be Child it calls child method and when you call Parent it uses Parent method.

When you create object of Child, Child and Parent have common method suppose display().

Parent a = new Child();
a.display(); 

It calls runtime child class method instead of parent class.

Edit:

public class Parent {

    public void display() {

        System.out.println("Parent-----Display");

    }
}


public class Child extends Parent{

    public void display() {
        System.out.println("This is child method.");
    }

    public static void main(String[] args) {
        Parent parent=new Child();
        parent.display();
    }
}

Output:

This is child method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM