Suppose that you had the following classes Class D extends B
Suppose that you had the following classes.
Class D extends B { /* assume some code here */ }
Class C extends B { /* assume code here */ }
Class B extends A { /* some code here */ }
Class E extends A { /* assume some code here */ }
Class A { /* assume some code here */ }
Further suppose that we have the following variable declarations and assume that they have all been assigned a legal value of the appropriate type.
A [ ] arrayA = new A[ 20 ];
C [ ] arrayC = new C[20];
A varA;
B varB;
C varC;
D varD;
E varE;
Circle any of the following pieces of code that would cause compilation errors.
a- varA = varE;
b- varE = varA;
c- varE = (E ) varA;
d- for (int x = 0; x < 20; x++ )
arrayA[x] = arrayC[x];
e- for (int x = 0; x < 20; x++ )
arrayC[x] = arrayA[x];
f- varB = varE;
g- varB = varD;
h- arrayC[3] = varC;
i- arrayA[5] = varD;
j- varA = varD;
Solution
a- varA = varE;
it is fine, because E exteds A, so a Parent class can reference child class object
b- varE = varA;
Compile Time Error : a child class can not reference its parent without typecasting
c- varE = (E ) varA;
Fine
d- for (int x = 0; x < 20; x++ )
arrayA[x] = arrayC[x];
Fine. C is child of A
e- for (int x = 0; x < 20; x++ )
arrayC[x] = arrayA[x];
Compile Time Error : a child class can not reference its parent without typecasting
f- varB = varE;
Compile Error : There is no child/parent relation between B and E
g- varB = varD;
Fine
h- arrayC[3] = varC;
Fine
i- arrayA[5] = varD;
Fine. A is parent of D
j- varA = varD;
Fine. A is parent of D

