I have a code snippet like this ... :-
class A
{
public void getA()
{
System.out.prin tln("In A...");
}
}
class B extends A
{
public A getB()
{
C c1 = new C();
return (A) c1;
}
}
class C extends B
{
public void getC()
{
System.out.prin tln("In C...");
}
}
public class D
{
public static void main(String args[])
{
B b1 = new B();
A a1 = b1.getB();
a1.getA();
}
}
and I get result from that code snippet like....
"In A...".
But Now I change this code like following....
class A
{
public void getA()
{
System.out.prin tln("In A...");
}
}
class B extends A
{
C c1 = new C();
public A getB()
{
return (A) c1;
}
}
class C extends B
{
public void getC()
{
System.out.prin tln("In C...");
}
}
public class D
{
public static void main(String args[])
{
B b1 = new B();
A a1 = b1.getB();
a1.getA();
}
}
and I get some runtime error...
Why?
class A
{
public void getA()
{
System.out.prin tln("In A...");
}
}
class B extends A
{
public A getB()
{
C c1 = new C();
return (A) c1;
}
}
class C extends B
{
public void getC()
{
System.out.prin tln("In C...");
}
}
public class D
{
public static void main(String args[])
{
B b1 = new B();
A a1 = b1.getB();
a1.getA();
}
}
and I get result from that code snippet like....
"In A...".
But Now I change this code like following....
class A
{
public void getA()
{
System.out.prin tln("In A...");
}
}
class B extends A
{
//here I change
public A getB()
{
return (A) c1;
}
}
class C extends B
{
public void getC()
{
System.out.prin tln("In C...");
}
}
public class D
{
public static void main(String args[])
{
B b1 = new B();
A a1 = b1.getB();
a1.getA();
}
}
and I get some runtime error...
Why?
Comment