Hi,
I have a stupid question. Trying to teach myself about Java Inheritance. I have the following classes:
public class A { private int iClassVariableA; public set_class_VariableA( int iVariable) { iClassVariableA = iVariable; } } public class B extends A { private int iClassVariableB; public set_class_VariableB( int iVariable) { iClassVariableB = iVariable; } } public class C extends A { private int iClassVariableC; public set_class_VariableC( int iVariable) { iClassVariableC = iVariable; } }
In my main I have the following:
A[] myA = new A[3]; myA[0] = new A(); myA[1] = new B(); myA[2] = new C()'; myA[0].set_class_VariableA(0); myA[1].set_class_VariableA(0); myA[1].set_class_VariableB(1); // I get a compiler error here myA[2].set_class_VariableA(0); myA[2].set_class_VariableC(2); // and here
The error is:
cannot find symbol
myA[1].set_class_VariableB(1);
^
symbol: method set_class_VariableB(int)
location: class A
If I create each instance variable as it's own type:
myA = new A(); myB = new B(); myC = new C()';
everything works fine. My understanding of Java's inheritance says that I should be able to create a variable of class A and assign it to class B... What am I not understanding here?
Thanks,
Tim