This document contains code from multiple Java classes that demonstrate access modifiers. Class Protection defines fields with different access levels - private, protected, public. Derived extends Protection and can access protected and public fields. SamePackage instantiates Protection and can access protected and public fields. Protection2 in another package extends Protection and can only access public fields. OtherPackage in another package instantiates Protection and can only access public fields.
This document contains code from multiple Java classes that demonstrate access modifiers. Class Protection defines fields with different access levels - private, protected, public. Derived extends Protection and can access protected and public fields. SamePackage instantiates Protection and can access protected and public fields. Protection2 in another package extends Protection and can only access public fields. OtherPackage in another package instantiates Protection and can only access public fields.
This document contains code from multiple Java classes that demonstrate access modifiers. Class Protection defines fields with different access levels - private, protected, public. Derived extends Protection and can access protected and public fields. SamePackage instantiates Protection and can access protected and public fields. Protection2 in another package extends Protection and can only access public fields. OtherPackage in another package instantiates Protection and can only access public fields.
public class Protection { class Derived extends Protection { int n = 1; Derived() { private int n_pri = 2; System.out.println("derived constructor"); System.out.println("n = " + n); protected int n_pro = 3; // class only public int n_pub = 4; // System.out.println("n_pri = " + n_pri); public Protection() { System.out.println("n_pro = " + n_pro); System.out.println("base constructor"); System.out.println("n_pub = " + n_pub); System.out.println("n = " + n); } System.out.println("n_pri = " + n_pri); } System.out.println("n_pro = " + n_pro); This is file Derived.java: System.out.println("n_pub = " + n_pub); } }
This is file Protection.java:
Program 2 contd… package p1; package p2; class SamePackage { class Protection2 extends p1.Protection { SamePackage() { Protection2() { Protection p = new Protection(); System.out.println("derived other package System.out.println("same package constructor"); constructor"); System.out.println("n = " + p.n); // class or package only // class only // System.out.println("n = " + n); // System.out.println("n_pri = " + p.n_pri); // class only System.out.println("n_pro = " + p.n_pro); // System.out.println("n_pri = " + n_pri); System.out.println("n_pub = " + p.n_pub); System.out.println("n_pro = " + n_pro); } System.out.println("n_pub = " + n_pub); } } }
This is file Protection2.java:
This is file SamePackage.java Program 2 contd… // Demo package p1. package p2; package p1; class OtherPackage { // Instantiate the various classes in p1. OtherPackage() { public class Demo { p1.Protection p = new p1.Protection(); public static void main(String args[]) { System.out.println("other package constructor"); Protection ob1 = new Protection(); // class or package only Derived ob2 = new Derived(); // System.out.println("n = " + p.n); SamePackage ob3 = new SamePackage(); // class only // System.out.println("n_pri = " + p.n_pri); } // class, subclass or package only } // System.out.println("n_pro = " + p.n_pro); System.out.println("n_pub = " + p.n_pub); // Demo package p2. } package p2; } // Instantiate the various classes in p2. public class Demo { public static void main(String args[]) { Protection2 ob1 = new Protection2(); This is file OtherPackage.java: OtherPackage ob2 = new OtherPackage(); } }