What is the difference between up-casting and down-casting with respect to class variable

后端 未结 10 1748
暗喜
暗喜 2020-11-22 09:55

What is the difference between up-casting and down-casting with respect to class variable?

For example in the following program class Animal contains only one method

相关标签:
10条回答
  • 2020-11-22 10:00

    upcasting means casting the object to a supertype, while downcasting means casting to a subtype.

    In java, upcasting is not necessary as it's done automatically. And it's usually referred as implicit casting. You can specify it to make it clear to others.

    Thus, writing

    Animal a = (Animal)d;
    

    or

    Animal a = d;
    

    leads to exactly the same point and in both cases will be executed the callme() from Dog.

    Downcasting is instead necessary because you defined a as object of Animal. Currently you know it's a Dog, but java has no guarantees it's. Actually at runtime it could be different and java will throw a ClassCastException, would that happen. Of course it's not the case of your very sample example. If you wouldn't cast a to Animal, java couldn't even compile the application because Animal doesn't have method callme2().

    In your example you cannot reach the code of callme() of Animal from UseAnimlas (because Dog overwrite it) unless the method would be as follow:

    class Dog extends Animal 
    { 
        public void callme()
        {
            super.callme();
            System.out.println("In callme of Dog");
        }
        ... 
    } 
    
    0 讨论(0)
  • 2020-11-22 10:02

    Parent: Car
    Child: Figo
    Car c1 = new Figo();

    =====
    Upcasting:-
    Method: Object c1 will refer to Methods of Class (Figo - Method must be overridden) because class "Figo" is specified with "new".
    Instance Variable: Object c1 will refer to instance variable of Declaration Class ("Car").

    When Declaration class is parent and object is created of child then implicit casting happens which is "Upcasting".

    ======
    Downcasting:-
    Figo f1 = (Figo) c1; //
    Method: Object f1 will refer to Method of Class (figo) as initial object c1 is created with class "Figo". but once down casting is done, methods which are only present in class "Figo" can also be referred by variable f1.
    Instance Variable: Object f1 will not refer to instance variable of Declaration class of object c1 (declaration class for c1 is CAR) but with down casting it will refer to instance variables of class Figo.

    ======
    Use: When Object is of Child Class and declaration class is Parent and Child class wants to access Instance variable of it's own class and not of parent class then it can be done with "Downcasting".

    0 讨论(0)
  • 2020-11-22 10:05

    I know this question asked quite long time ago but for the new users of this question. Please read this article where contains complete description on upcasting, downcasting and use of instanceof operator

    • There's no need to upcast manually, it happens on its own:

      Mammal m = (Mammal)new Cat(); equals to Mammal m = new Cat();

    • But downcasting must always be done manually:

      Cat c1 = new Cat();      
      Animal a = c1;      //automatic upcasting to Animal
      Cat c2 = (Cat) a;    //manual downcasting back to a Cat
      

    Why is that so, that upcasting is automatical, but downcasting must be manual? Well, you see, upcasting can never fail. But if you have a group of different Animals and want to downcast them all to a Cat, then there's a chance, that some of these Animals are actually Dogs, and process fails, by throwing ClassCastException. This is where is should introduce an useful feature called "instanceof", which tests if an object is instance of some Class.

     Cat c1 = new Cat();         
        Animal a = c1;       //upcasting to Animal
        if(a instanceof Cat){ // testing if the Animal is a Cat
            System.out.println("It's a Cat! Now i can safely downcast it to a Cat, without a fear of failure.");        
            Cat c2 = (Cat)a;
        }
    

    For more information please read this article

    0 讨论(0)
  • 2020-11-22 10:08

    Down-casting and up-casting was as follows:

    Upcasting: When we want to cast a Sub class to Super class, we use Upcasting(or widening). It happens automatically, no need to do anything explicitly.

    Downcasting : When we want to cast a Super class to Sub class, we use Downcasting(or narrowing), and Downcasting is not directly possible in Java, explicitly we have to do.

    Dog d = new Dog();
    Animal a = (Animal) d; //Explicitly you have done upcasting. Actually no need, we can directly type cast like Animal a = d; compiler now treat Dog as Animal but still it is Dog even after upcasting
    d.callme();
    a.callme(); // It calls Dog's method even though we use Animal reference.
    ((Dog) a).callme2(); // Downcasting: Compiler does know Animal it is, In order to use Dog methods, we have to do typecast explicitly.
    // Internally if it is not a Dog object it throws ClassCastException
    
    0 讨论(0)
  • 2020-11-22 10:09

    Maybe this table helps. Calling the callme() method of class Parent or class Child. As a principle:

    UPCASTING --> Hiding

    DOWNCASTING --> Revealing

    enter image description here

    enter image description here

    enter image description here

    0 讨论(0)
  • 2020-11-22 10:10

    We can create object to Downcasting. In this type also. : calling the base class methods

    Animal a=new Dog();
    a.callme();
    ((Dog)a).callme2();
    
    0 讨论(0)
提交回复
热议问题