The Program is:
class A
{
int i = 10;
}
class B extends A
{
int j = 20;
}
class C extends B
{
int k = 30;
}
class D extends C
{
int m = 40;
}
public class asg2
{
public static void main(String[] args)
{
A[] a = {new A(),new B(), new C(), new D()};
System.out.println(a[3].i); //No error!!!
System.out.println(a[2].j); //throws error
System.out.println(a[1].k); //throws error (Understood why error)
System.out.println(a[0].m); //throws error (Understood why error)
}
}
I understand why the last two throw error. But I dont understand why the 2nd print statement throws error. And first one runs smoothly.
asg2.java:29: error: cannot find symbol
System.out.println(a[2].j);
^
symbol: variable j
location: class A