//Chapter 9 inheritance stuff crammed into here //part 4 public class Inheritance4 { public static void main( String[] args ) { X x1 = new X(); Y y1 = new Y(); //System.out.println( "x1.i_priv= " + x1.i_priv ); //syntax error System.out.println( "x1.getI_priv()= " + x1.getI_priv() ); //System.out.println( "y1.i_priv= " + y1.i_priv ); //syntax error //protected accessible because in same package... System.out.println( "x1.i_prot= " + x1.i_prot ); System.out.println( "y1.i_prot= " + y1.i_prot ); y1.m2(); System.out.println( "x1.i_prot= " + x1.i_prot ); System.out.println( "y1.i_prot= " + y1.i_prot ); } } class X { private int i_priv=3; //not accessible to any classes, incl. subclasses //protected member accessible to subclasses and package // but not to other classes. //sort of public to subclasses and package but private to other classes. protected int i_prot=3; //Deitel says avoid protected, use private fields with non-private accessors //and mutators. better if subclass not dependent on superclass implementation //(allows superclass to change implementation without requiring subclass change) //protected breaks the encapsulation of the superclass? public int getI_priv() { return i_priv; } } class Y extends X { void m2() { i_prot = 12; //inherited field. //i_priv = 13; //syntax error. inherits it but is inaccessible. System.out.println( "hello from Y m2()" ); } } // protected field of subclass not accessible to (other-package) superclass. // members accessible to an object are alos accessible from other objects of // the same class. Access is "class-based" not "object-based"