/* test various tricky aspects of constructors */ public class mainer { public static void main(String[]args) { System.out.println("Test chaining 1..4"); C x = new C(1,2,3); System.out.println("checking"); x.check(1,2,3); System.out.println("testing dynamic dispatch foo"); x.doit(); System.out.println("testing interface call"); F f = x; f.check(1,2,3); f.doit(); System.out.println("Test immutable default constructor chaining 1..2"); I i1; System.out.println("checking"); i1.check(10,20,30); System.out.println("Test immutable constructor chaining 1..3"); I i2 = new I(1,2,3); System.out.println("checking"); i2.check(1,2,3); D d = new D(); System.out.println("testing default constructor"); d.callsync(); System.out.println("ok"); E e = new E(13); System.out.println("testing this&super arg lowering"); e.check(13); System.out.println("done"); } } interface F { public void check(int a, int b, int c); public void doit(); } class A { public int x = 10; public int a = 0; public A(int a) { this.a = a; System.out.println("ok1"); } public void foo() { System.out.println("okfoo"); foo2(); } public void foo2() { System.out.println("wrongfoo2!!!"); } } class B extends A { public int y = 20; public int b = 0; public B(int a, int b) { super(a); this.b = b; System.out.println("ok2"); } public void bogus1() { System.out.println("wrong method!!!"); } public void bogus2() { System.out.println("wrong method!!!"); } } class C extends B implements F { public int z = 30; public int c = 0; public C(int a, int b, int c) { this(a,b,c,0); System.out.println("ok4"); } private C(int a, int b, int c, int junk) { super(a,b); this.c = c; System.out.println("ok3"); } public void foo() { System.out.println("wrong method!!!"); } public void doit_() { super.foo(); } public void foo2() { System.out.println("okfoo2"); } public void doit() { this.doit_(); } public void check(int a, int b, int c) { if (x != 10) System.out.println("wrong x"); if (y != 20) System.out.println("wrong y"); if (z != 30) System.out.println("wrong z"); if (this.a != a) System.out.println("wrong a"); if (this.b != b) System.out.println("wrong b"); if (this.c != c) System.out.println("wrong c"); } } immutable class I { public int a; public int b; public int c; // bug here in original compiler - leave off for now //public int d = 101; //public int e = 102; public I(double f) { System.out.println("okdI1"); a = 10; b = 20; c = 30; } public I() { this(1.1); System.out.println("okdI2"); } public I(int a, int b, int c) { this(a,b); System.out.println("okI3"); this.c = c; } public I(int a, int b) { this(a); System.out.println("okI2"); this.b = b; } public I(int a) { this.a = a; System.out.println("okI1"); } public void check(int a, int b, int c) { if (this.a != a) System.out.println("wrong a"); if (this.b != b) System.out.println("wrong b"); if (this.c != c) System.out.println("wrong c"); //if (this.d != 101) System.out.println("wrong d"); //if (this.e != 102) System.out.println("wrong e"); } } class D { int x = 40; public synchronized void callsync() { if (x != 40) System.out.println("wrong D.x"); } } class E extends A { public int r = -1; public E(int r) { this(r+10,0); } private E(int r,int junk) { super(r+10); this.r = r; } public void check(int r) { if (this.r != r + 10) System.out.println("wrong r"); if (this.a != r + 20) System.out.println("wrong a"); } }