class Superclass
{
void display()
{
System.out.println("Super");
}
}
class Sub1 extends Superclass
{
void display()
{
System.out.println("sub class1");
}
}
class Sub2 extends Sub1
{
void display()
{
System.out.println("Sub class2");
}
void display2()
{
System.out.println("Second method");
}
}
public class reference {
public static void main(String[] args)
{
Superclass ref=new Sub1();
ref.display();
((Sub2)ref).display();
}
}
I am trying to invoke a Sub2 class specific method using Superclass reference but throws a ClassCastException.Any help in explaining why??