Downcasting in java
-------------------------------------------------------------------------
Downcasting executes in the following class "A"
class A
{
public static void main(String ss[])
{
int p=2;
short d=(short)p; //-------------------->downcasting
System.out.println((short)d);
}
}
But a run time exception occurs in the following case
public class Redwood extends Tree
{
public static void main(String s[])
{
new Redwood().go();
}
void go()
{
go2(new Tree(),new Redwood());
go2((Redwood) new Tree(),new Redwood());//------------------->downcasting
}
void go2(Tree t1,Redwood r1)
{
Redwood r2=(Redwood)t1;
Tree t2=(Tree)r1;
}
}
class Tree{}
----------------------------------------------------------------------
Please assist.
Thanks