简体   繁体   English

Java - 向上转发和向下转发

[英]Java - Upcasting and Downcasting

I Knew there are plenty of articles/questions in stackoverflow describing about upcasting and downcasting in Java. 我知道stackoverflow中有很多文章/问题描述了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() . 如果你看一下Animal and Dog类,每个类都有自己的方法,比如getAnimalName()getDogName() Hence Dog extends Animal (is-a relationship), so we can use the base class(Super Class) methods in the derived class(Subclass) 因此Dog extends Animal (is-a relationship),所以我们可以在派生类(Subclass)中使用基类(Super Class)方法

Consider the below piece of code in the Main Method now, 现在考虑主方法中的下面一段代码,
So here I'm creating a Dog object w.rt Animal . 所以我在这里创建一个Dog对象w.rt Animal So I can be able to access only the Animal properties(Methods) 所以我只能访问Animal属性(方法)

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 wrt Animal , but here it is printing the base class method which is overridden in the dervied class. 即使我正在创建一个Dog对象wrt Animal ,但是这里它正在打印基类方法,该方法在dervied类中被重写。

 O/P : Parent Animal overridden here<br>

Wondering why it behaves like this. 想知道它为什么会这样。 Is this becasue of the override ? 这是因为override吗?

Please provide your valuable input's. 请提供您宝贵的意见。

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(); 这里a.getAnimalName(); calls the subclass's getAnimalName() method, as it is inherited from base class, so the parent class's method is called. 调用子类的getAnimalName()方法,因为它是从基类继承的,因此调用父类的方法。 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. 但需要注意的是,基类引用不能调用子类上的方法,这些方法没有在基类中定义。

Animal a = d; 

this line will make your Animal object 'a' point to the instance of Dog class (Dog d = new Dog();). 这一行将使你的Animal对象'a'指向Dog类的实例(Dog d = new Dog();)。 Therefore when you call the function it will invoke the function in class dog. 因此,当您调用该函数时,它将调用类dog中的函数。

You actually created an instance of class Dog Dog d = new Dog(); 你实际创建了一个Dog Dog d = new 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; 然后你正在创建一个Animal类的对象,并使它指向类Dog Animal a = d;的实例Animal a = d;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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