Header Ads

  • Breaking Now

    Give an example where upcasting takes advantage of polymorphism.

    Here it goes:

    class Parent { }

    class Child extends Parent { }

    Child can be referred as an object of type Parent or Child.

    Upcasting works like this:

    Child aChildObj = new Child();

    Parent aParentObj = (Parent)aChildObj;

    As aChildObj can be referred to by the class names Child and
    Parent,you can say that this upcasting and type extension is the
    implementation of polymorphism in Java.

    Post Top Ad

    Post Bottom Ad