/*****************************************************/ // Methods // + use the instance variables from the classes in // which the instance variables are written. // + are accessed from the actual objects, not // the declared types of the references! // + are copied into the subclass if public and // not overriden (written with same header in // the subclass) // Constructor code (body of constructors): // Acts like method -- accesses instance variables // of class in which the constructor is written class A { public int x; public A(int x) { this.x = x; System.out.println("x from the A constr: "+x); } public void show() {System.out.println("x! "+x);} } class B extends A { public B(int x) { super(x-1); this.x=x; } } public class inherit4 { 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(); } } /* sample output: Test 1: x from the A constr: 1 v1 uses class B accessing x: 2 x! 2 Test 2: x from the A constr: 1 v2 uses class B accessing x: 2 x! 2 */