Hi,
We always have to remember that
polymorphism will take place for overriding methods when we do some casting. If you have someother method in c1 and if you cast c2 to c1 at that time the object will call the method in c1.
class c1
{
public int add(){return 2+3;}
public int sub(){return 10-5;}
}
class c2 extends c1
{
public int add(){return 2*3;}
}
class c3 extends c2
{
public int add(){return 100/5;}
void test()
{
c1 c2obj = new c2();
c1 c3obj = new c3();
System.out.println("add()="+add());
System.out.println("super.add()"+super.add());
System.out.print("((c2)this).add()=");
System.out.println(((c2)this).add());
System.out.print("((c1)this).add()=\t");
System.out.println(((c1)this).add());
System.out.print("((c1)this).sub()=\t");
System.out.println(((c1)this).sub());
System.out.print("((c1)c2obj).add()=\t");
System.out.println(((c1)c2obj).add());
System.out.print("((c1)c3obj).add()=\t");
System.out.println(((c1)c3obj).add());
}
}
public class testAgain
{
public static void main(String[] args)
{
c3 myC3 = new c3();
myC3.test();
}
}