/** * This code is from the book: * Winder, R and Roberts, G (1998) * Developing Java Software</em> * John Wiley & Sons. * It is copyright (c) 1997 Russel Winder * and Graham Roberts. */ class A{ protected static String name = "A"; public void f(){ System.out.println(name + " f"); } } class Superclass{ protected static String name = "Superclass"; protected String vname = "Superclass instance variable"; public void name(){ System.out.println(name); } } class Subclass extends Superclass{ protected static String name = "Subclass"; protected String vname = "Subclass instance variable"; private class Member extends A{ //Member class can inherit public void show(){ //instance variable //OK as instance name hides any inherited //or contained name System.out.println(name); //Variables can be explicitly used //from each scope using this syntax System.out.println(Subclass.this.name); System.out.println(Superclass.this.name); System.out.println(A.this.name); System.out.println(Subclass.this.vname); System.out.println(Superclass.this.vname); //No conflict here as only containment //hierarchy has a vname System.out.println(vname); //Containing object methods can be called. Subclass.this.name(); Superclass.this.name(); //If this is omitted then method in the containing //object can still be accessed. As no method name //is inherited there is also no conflict name(); //Ambiguous due to name conflict between inheritance //and containment //f(); //Error A.this.f(); Subclass.this.f(); } public void test(int x){ System.out.println(name + " test"); } private String name = "Member"; } public void name(){ System.out.println(name); } public void test(){ Member m = new Member(); m.show(); } public void f(){ System.out.println("Subclass:f"); } } class l1Inherit6{//Lookup1 public static void main(String[] args){ Subclass subclass = new Subclass(); subclass.test(); } } /******** sample compilation & run ******* # javac l1Inherit6.java # java l1Inherit6 Member Subclass Superclass A Subclass instance variable Superclass instance variable Subclass instance variable Subclass Subclass Subclass A f Subclass:f # ******************************************/