Java - Upcasting and Downcasting

帅比萌擦擦* 提交于 2020-01-01 19:40:16

问题


I Knew there are plenty of articles/questions in stackoverflow describing about upcasting and downcasting in Java. And I knew what is upcasting and downcasting. But my question is not specfic to that.

Upcasting - Conversion from child to parent - Compiler takes care. No cast is required
Downcasting - Conversion from parent to child - Explicit cast is required

public class Animal {

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

    public void getDogName(){
        System.out.println("Dog");
    }
}

public static void main(String[] args) {

        Dog d = new Dog();
        Animal a = d; // Upcasting
        a.getAnimalName();

        Animal vv = new Dog();
        Dog cc = (Dog)vv;//DownCasting
        cc.getAnimalName();
        cc.getDogName();



If you look into the Animal and Dog class, each are having their own methods like getAnimalName() and getDogName(). Hence Dog extends Animal(is-a relationship), so we can use the base class(Super Class) methods in the derived class(Subclass)

Consider the below piece of code in the Main Method now,
So here I'm creating a Dog object w.rt Animal. So I can be able to access only the Animal properties(Methods)

Dog d = new Dog();
Animal a = d; // Upcasting
a.getAnimalName();<br>
O/P : Parent Animal<br><br>

Now Let's say, I would like to override the base class methods into the derived class

public class Dog extends Animal{

    @Override
    public void getAnimalName(){
        System.out.println("Parent Animal overridden here");
    }
    public void getDogName(){
        System.out.println("Dog");
    }
}<br>

And in Main Method,

Dog d = new Dog();
Animal a = d; // Upcasting
a.getAnimalName();<br>
O/P : Parent Animal overridden here<br><br>

Even though I'm creating a Dog object w.r.t Animal, but here it is printing the base class method which is overridden in the dervied class.

 O/P : Parent Animal overridden here<br>

Wondering why it behaves like this. Is this becasue of the override?

Please provide your valuable input's.


回答1:


When you refer your subclass with parent class, method call on reference pointer calls the subclass method.

Dog d = new Dog();
Animal a = d; // Upcasting
a.getAnimalName();

Here a.getAnimalName(); calls the subclass's getAnimalName() method, as it is inherited from base class, so the parent class's method is called. It is not directly called on base class, rather through subclass's inheritance. When you override, it is instantly invoked from subclass, it does not need to go to parent class to check the existence of the method.

But a side note is that base class reference can't call methods on subclass, which are not defined in base class.




回答2:


Animal a = d; 

this line will make your Animal object 'a' point to the instance of Dog class (Dog d = new Dog();). Therefore when you call the function it will invoke the function in class dog.

You actually created an instance of class Dog Dog d = new Dog();. Then you are making an object of class Animal and making it point to the instance of class Dog Animal a = d;



来源:https://stackoverflow.com/questions/40393875/java-upcasting-and-downcasting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!