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
?
Let's start from the beginning: This is terrible code. That being said:
Some notes:
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);
}