Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Downcasting in Java

Tags:

java

casting

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.

In this case why Java allows downcasting if it cannot be executed at the runtime?
Is there any practical use for this concept?

public class demo {   public static void main(String a[]) {       B b = (B) new A(); // compiles with the cast,                           // but runtime exception - java.lang.ClassCastException   } }  class A {   public void draw() {     System.out.println("1");   }    public void draw1() {     System.out.println("2");   } }  class B extends A {   public void draw() {     System.out.println("3");   }   public void draw2() {     System.out.println("4");   } } 
like image 560
Warrior Avatar asked Dec 19 '08 12:12

Warrior


People also ask

What is the downcasting in Java?

Downcasting is another form of object type casting and is used in appointing the subclass reference object to the parent class that is not allowed in Java.

What is downcasting used for?

Downcasting is useful when the type of the value referenced by the Parent variable is known and often is used when passing a value as a parameter. In the below example, the method objectToString takes an Object parameter which is assumed to be of type String.

What is meant by Upcasting and downcasting in Java?

What are Upcasting and Downcasting in Java? Upcasting (Generalization or Widening) is casting to a parent type in simple words casting individual type to one common type is called upcasting while downcasting (specialization or narrowing) is casting to a child type or casting common type to individual type.

Is downcasting safe in Java?

Upcasting is always safe and never fails. Downcasting can risk throwing a ClassCastException, so the instanceof operator is used to check type before casting.


2 Answers

Downcasting is allowed when there is a possibility that it succeeds at run time:

Object o = getSomeObject(), String s = (String) o; // this is allowed because o could reference a String 

In some cases this will not succeed:

Object o = new Object(); String s = (String) o; // this will fail at runtime, because o doesn't reference a String 

When a cast (such as this last one) fails at runtime a ClassCastException will be thrown.

In other cases it will work:

Object o = "a String"; String s = (String) o; // this will work, since o references a String 

Note that some casts will be disallowed at compile time, because they will never succeed at all:

Integer i = getSomeInteger(); String s = (String) i; // the compiler will not allow this, since i can never reference a String. 
like image 86
Joachim Sauer Avatar answered Sep 25 '22 19:09

Joachim Sauer


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(); } 
like image 36
Rolf Rander Avatar answered Sep 26 '22 19:09

Rolf Rander