I'm creating some random classes to understand Polymorphism better. Coding is as follows:
Poly1:
public abstract class Poly1 {
int comPoly;
}
SubPoly1:
public class SubPoly1 extends Poly1 {
String testPoly;
}
SubPoly2:
public class SubPoly2 extends Poly1 {
int x;
}
testPoly:
public class testPoly {
public static void main(String[] args) {
Poly1[] testObj = new Poly1[2];
testObj[0] = new SubPoly1();
testObj[1] = new SubPoly2();
testObj[1].x = 1;
testObj[1].comPoly = 2;
System.out.println("Test Output : " + testObj[1].x+ " and " + testObj[1].comPoly);
testObj[0].testPoly = "Hello";
testObj[0].comPoly = 8;
System.out.println("Test Output : " + testObj[0].testPoly+ " and " + testObj[1].comPoly);
}
}
But the program is not getting past the compilation stage as I get the symbol not found
error whenever I try to access the variables from SubPoly1
or SubPoly2
(for example testObj[1].x
would return an error).
Any help would be appreciated.