I have 3 very simple classes and a question about their inheritance.
public class A {
int a;
A(int a){
this.a = a;
}
void meetoda(){
System.out.println("a = " + a);
}
@Override
public String toString() {
return "A [a=" + a + "]";
}
}
public class B extends A {
int b;
B(int a, int b){
super(a);
this.b = b;
}
void meetodb(){
System.out.println("b = " + b);
}
@Override
public String toString() {
return "B [b=" + a + ", " + b + "]";
}
}
public class TestABC {
public static void main(String[] args) {
A a3 = new A(45);
B b3 = new B(34,28);
a3 = b3;
System.out.println(a3);
}
}
When I print out a3, I get "B [b=34, 28]", but when I try to print a3.b, I get an error. Why?