Can someone please make me clear the concept of 'ClassCastException'.
Consider this example :
class superA
{
public void hello()
{
System.out.println("Hello");
}
}
public class subClass extends superA
{
public static void main(
String ar[])
{
Object obj = "hello";// 1
String a=(String) obj;// 2
System.out.println(a);
superA supA = new superA(); // 3
subClass objRef =(subClass) supA;// 4
}
}
In 3 a superclass reference is created and i am assigning to a subclass reference.
Same is done in the case of 1, But(2) it is not showing a runtime error where as (4) is
showing an runtime error. Why???