// INHERIT5 // See $show$ below. This method is accessed from // the actual object, which means that even though the // code for $show$ is written in the superclass, // you have to pretend it is written in the subclass! class A { public int x; public A(int x) { this.x = x; System.out.println("x from the A constr: "+x); show(); } public void show() {System.out.println("(A): "+x);} } class B extends A { public B(int x) { super(x-1); this.x=x; show(); } } public class inherit5 { public static void main(String[] args){ System.out.println("\nTest 1:"); B v1 = new B(2); System.out.println("v1 uses "+v1.getClass()); System.out.println("accessing x: "+v1.x); v1.show(); System.out.println("\nTest 2:"); A v2 = new B(2); System.out.println("v2 uses "+v2.getClass()); System.out.println("accessing x: "+v2.x); v2.show(); } } /* output: Test 1: x from the A constr: 1 (A): 1 (A): 2 v1 uses class B accessing x: 2 (A): 2 Test 2: x from the A constr: 1 (A): 1 (A): 2 v2 uses class B accessing x: 2 (A): 2 */