Downcasting in Java

后端 未结 11 2120
自闭症患者
自闭症患者 2020-11-22 03:15

Upcasting is allowed in Java, however downcasting gives a compile error.

The compile error can be removed by adding a cast but would anyway break at the runtime.

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

    We can all see that the code you provided won't work at run time. That's because we know that the expression new A() can never be an object of type B.

    But that's not how the compiler sees it. By the time the compiler is checking whether the cast is allowed, it just sees this:

    variable_of_type_B = (B)expression_of_type_A;
    

    And as others have demonstrated, that sort of cast is perfectly legal. The expression on the right could very well evaluate to an object of type B. The compiler sees that A and B have a subtype relation, so with the "expression" view of the code, the cast might work.

    The compiler does not consider the special case when it knows exactly what object type expression_of_type_A will really have. It just sees the static type as A and considers the dynamic type could be A or any descendant of A, including B.

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

    Downcasting transformation of objects is not possible. Only

    DownCasting1 _downCasting1 = (DownCasting1)((DownCasting2)downCasting1);
    

    is posible

    class DownCasting0 {
        public int qwe() {
            System.out.println("DownCasting0");
            return -0;
        }
    }
    
    class DownCasting1 extends DownCasting0 {
        public int qwe1() {
            System.out.println("DownCasting1");
            return -1;
        }
    }
    
    class DownCasting2 extends DownCasting1 {
        public int qwe2() {
            System.out.println("DownCasting2");
            return -2;
        }
    }
    
    public class DownCasting {
    
        public static void main(String[] args) {
    
            try {
                DownCasting0 downCasting0 = new DownCasting0();
                DownCasting1 downCasting1 = new DownCasting1();
                DownCasting2 downCasting2 = new DownCasting2();
    
                DownCasting0 a1 = (DownCasting0) downCasting2;
                a1.qwe(); //good
    
                System.out.println(downCasting0 instanceof  DownCasting2);  //false
                System.out.println(downCasting1 instanceof  DownCasting2);  //false
                System.out.println(downCasting0 instanceof  DownCasting1);  //false
    
                DownCasting2 _downCasting1= (DownCasting2)downCasting1;     //good
                DownCasting1 __downCasting1 = (DownCasting1)_downCasting1;  //good
                DownCasting2 a3 = (DownCasting2) downCasting0; // java.lang.ClassCastException
    
                if(downCasting0 instanceof  DownCasting2){ //false
                    DownCasting2 a2 = (DownCasting2) downCasting0;
                    a2.qwe(); //error
                }
    
                byte b1 = 127;
                short b2 =32_767;
                int b3 = 2_147_483_647;
    //          long _b4 = 9_223_372_036_854_775_807; //int large number max 2_147_483_647
                long b4 = 9_223_372_036_854_775_807L;
    //          float _b5 = 3.4e+038; //double default
                float b5 = 3.4e+038F; //Sufficient for storing 6 to 7 decimal digits
                double b6 = 1.7e+038;
                double b7 = 1.7e+038D; //Sufficient for storing 15 decimal digits
    
                long c1 = b3;
                int c2 = (int)b4;
    
                //int       4 bytes     Stores whole numbers from -2_147_483_648 to 2_147_483_647
                //float     4 bytes     Stores fractional numbers from 3.4e−038 to 3.4e+038. Sufficient for storing 6 to 7 decimal digits
                float c3 = b3; //logic error
                double c4 = b4; //logic error
    
    
            } catch (Throwable e) {
                e.printStackTrace();
            }
        }
    
    }
    
    0 讨论(0)
  • 2020-11-22 03:23

    Using your example, you could do:

    public void doit(A a) {
        if(a instanceof B) {
            // needs to cast to B to access draw2 which isn't present in A
            // note that this is probably not a good OO-design, but that would
            // be out-of-scope for this discussion :)
            ((B)a).draw2();
        }
        a.draw();
    }
    
    0 讨论(0)
  • 2020-11-22 03:28

    To do downcasting in Java, and avoid run-time exceptions, take a reference of the following code:

    if (animal instanceof Dog) {
      Dog dogObject = (Dog) animal;
    }
    

    Here, Animal is the parent class and Dog is the child class.
    instanceof is a keyword that is used for checking if a reference variable is containing a given type of object reference or not.

    0 讨论(0)
  • 2020-11-22 03:31

    In this case why Java allows downcasting if it cannot be executed at the runtime?

    I believe this is because there is no way for the compiler to know at compile-time if the cast will succeed or not. For your example, it's simple to see that the cast will fail, but there are other times where it is not so clear.

    For instance, imagine that types B, C, and D all extend type A, and then a method public A getSomeA() returns an instance of either B, C or D depending on a randomly generated number. The compiler cannot know which exact run-time type will be returned by this method, so if you later cast the results to B, there is no way to know if the cast will succeed (or fail). Therefore the compiler has to assume casts will succeed.

    0 讨论(0)
  • 2020-11-22 03:33

    Downcasting is very useful in the following code snippet I use this all the time. Thus proving that downcasting is useful.

    private static String printAll(LinkedList c)
    {
        Object arr[]=c.toArray();
        String list_string="";
        for(int i=0;i<c.size();i++)
        {
            String mn=(String)arr[i];
            list_string+=(mn);
        }
        return list_string;
    }
    

    I store String in the Linked List. When I retrieve the elements of Linked List, Objects are returned. To access the elements as Strings(or any other Class Objects), downcasting helps me.

    Java allows us to compile downcast code trusting us that we are doing the wrong thing. Still if humans make a mistake, it is caught at runtime.

    0 讨论(0)
提交回复
热议问题