// IV4: continuing our exploration of how/whan Java sets instance variables. public class iv4 { public static void main(String[] args) { new B(1,2); } } class A { int x; int y=x; A(int a, int b) { System.out.println("A (xy1): "+x+" "+y); x = a; y = b; System.out.println("A (xy2): "+x+" "+y); } } class B extends A { int a=1; int b=a; B(int q, int r) { // Want to check $a$ and $b$, but can't until $super$ is called: // System.out.println("B (ab): "+a+" "+b); // If you remove the comments, you will get 2 errors: // 1. No constructor matching A() found in class A // (because Java automatically tries the default constructor // if you don't provide a call to the $super$ constructor) // 2. Constructor invocation must be the first thing in a method. // (becase you tried giving $super$ later in the code) // You may fix by writing an empty constructor in class $A$ or // move the code "later," as show below. super(q,r); System.out.println("B (xy1): "+x+" "+y); x = q; y = r; System.out.println("B (xy2): "+x+" "+y); System.out.println("B (ab1): "+a+" "+b); a = x; b = y; System.out.println("B (ab2): "+a+" "+b); } } /* output: A (xy1): 0 0 A (xy2): 1 2 B (xy1): 1 2 B (xy2): 1 2 B (ab1): 1 1 B (ab2): 1 2 */