1
class A{

}

public class Demo 
{
  public static void   main(String s[])
  {         
      Object o=(Object) new Demo();
      if (((A)(o)) instanceof Object) 
      {
           System.out.println("true");
      }
  }
}

I am getting Exception while running the class Demo.java:

java.lang.ClassCastException: Demo cannot be cast to A

How to downcast o reference to class A?

Akhilesh Dhar Dubey
  • 2,152
  • 2
  • 25
  • 39
  • 3
    Where's the "down" in your code? The hierarchies of `A` and `Demo` meet only at `Object`. – mikołak Nov 27 '13 at 10:49
  • 2
    `((A)(o)) instanceof Object` that's quite odd logic ;) – Thomas Nov 27 '13 at 10:50
  • There is no need to ever cast something to an `Object` - every class in java is a descendant of `Object`. For the same reason, the expression `x instanceof Object` is meaningless, as it is always `true` – Gyro Gearless Nov 27 '13 at 11:01

7 Answers7

4

You can do that only if Demo extends A, otherwise, you simply cannot cast a class object to any other type.

public class Demo extends A {
Rahul
  • 44,383
  • 11
  • 84
  • 103
1

Let's start from the beginning: This is terrible code. That being said:

  • You are casting Demo to Object (for whatever reason, since in Java everything is Object, no need to cast).
  • You are then casting o, that you know it's of type Demo, to A (why would this work?).
  • You are checking if Object o is of type Object (why would this fail?)

Some notes:

  • o should not be viewed as a reference, it is an instance of Object, as you declared it. Forget how things worked in C.
  • Consider interfaces and if you want A to be an interface that Demo implements.
  • You can only cast instances to a class that they already extend.

Downcast example:

    public class A {
    int variable = 0; 
}

public class Demo extends A{

}

public void testDowncast(){
    Demo myClass = new Demo();
    myClass.variable = 2;
    A morphingTime = myClass;
    System.out.println("And now Power Ranger Demo has turned into Mighty A:");
    System.out.println("I am: "+morphingTime.getClass() + " and my variable is: " + morphingTime.variable);
}
Ric Jafe
  • 2,321
  • 2
  • 18
  • 18
1

The answer by R J is right

you can do that only if Demo extends A

For your information, you do not need to type cast any object while assigning to Object

Object o= new Demo();

and every object will always be instanceof Object i.e. your condition instanceof Object for class objects will always be true

And why are you trying to do the things this way, ((A)(o)) without checking the type with instanceof rather it should be,

if (o instanceof A) 
Community
  • 1
  • 1
Deepak Bhatia
  • 6,230
  • 2
  • 24
  • 58
1

First of all, you'r getting 'ClassCastException' because your actual object 'o' is of type class 'Demo' and classes 'Demo' and 'A' are not in the same inheritance tree. You didn't get compile error only because you have cast your object to class 'Object' (since 'A' and 'Object' are in the same inheritance tree). To resolve you situation you should change you code such that make both of them ('Demo and 'A') to be part of the same inheritance tree. For example you can extend Demo from A. Then, check the object 'o' without cast like this

    if (o instanceof A) {
      // now cast to 'A' 
      // and invoke any accessible method (or etc.) that class A provides
      ((A)o).doSomthingMathod(); 
    }
sergeyan
  • 1,173
  • 1
  • 14
  • 28
  • Is there a way of doing this downcast without instanceof? Otherwise you must add new if statements with instanceof for each new type you add. – powder366 Sep 09 '17 at 11:58
0

First thing u do not need to cast your Demo instance to object becuase without casting you can assign the Demo reference to Object class because Object is super class of all java classes.

public class Demo extends com.A {

}
Deepak
  • 2,287
  • 1
  • 23
  • 30
0

You should simply rewrite your code as

Object o=new Demo();
  if (o instanceof A) 
  {
       System.out.println("true");
  }

and then see what changes if Demo extends A

Gyro Gearless
  • 5,131
  • 3
  • 18
  • 14
0

Downcasting is the act of casting a reference of a base class to one of its derived classes.

For example :

public class Parent{}
public class Child extends Parent{}

public static void main(String args[]){
Parent parent = new Child();              // Parent is parent class of Child, parent variable holding value of type Child
Child child = (Child)parent;              // This is possible since parent object is currently holding value of Child class
}

You can refer this question to get the answer.

Community
  • 1
  • 1
rachana
  • 3,344
  • 7
  • 30
  • 49