class A { public static void main(String[] args) { A a = new A(); B b = new B(); A ab = new B(); System.out.format("%d %d %d %d %d %d", ax, bx, ab.x, ay, by, ab.y); } int x = 2; int y = 3; A(int x) { this.x = x; } A() { this(7); } } class B extends A { int y = 4; B() { super(6); }
Hey there, I just looked through some examples from my course and came across this problem that surpassed me.
I understand that this code should print "7 6 6 3 4 3"
But why is ab.y equal to 3? Isn't the "real" type of an object of class B class ab? Which then would make me believe that ab.y be 4?
java
Kevin zhou
source share