by Dinesh Thakur

Access modifiers define the scope of the class and its members (data and methods). For example, private members are accessible within the same class members (methods). Java provides many levels of security that provides the visibility of members (variables and methods) within the classes, subclasses, and packages.

Packages are meant for encapsulating, it works as containers for classes and other subpackages. Class acts as containers for data and methods. There are four categories, provided by Java regarding the visibility of the class members between classes and packages:

 

  1. Subclasses in the same package
  2. Non-subclasses in the same package
  3. Subclasses in different packages
  4. Classes that are neither in the same package nor subclasses

 

The three main access modifiers private, public and protected provides a range of ways to access required by these categories.

                    ACCESS PROTECTION IN PACKAGES

Simply remember, private cannot be seen outside of its class, public can be access from anywhere, and protected can be accessible in subclass only in the hierarchy.

A class can have only two access modifier, one is default and another is public. If the class has default access then it can only be accessed within the same package by any other code. But if the class has public access then it can be access from any where by any other code.

 

Example:

//PCKG1_ClassOne.java

package pckg1;

public class PCKG1_ClassOne{

int a = 1;

private int pri_a = 2;

protected int pro_a = 3;

public int pub_a = 4;

public PCKG1_ClassOne() {

System.out.println("base class constructor called");

System.out.println("a = " + a);

System.out.println("pri_a = " + pri_a);

System.out.println("pro_a "+ pro_a);

System.out.println("pub_a "+ pub_a);

}

}

 

The above file PCKG1_ClassOne belongs to package pckg1, and contains data members with all access modifiers.

 

//PCKG1_ClassTwo.java

 

package pckg1;

class PCKG1_ClassTwo extends PCKG1_ClassOne {

PCKG1_ClassTwo() {

System.out.println("derived class constructor called");

System.out.println("a = " + a);

II accessible in same class only

II System.out.println("pri_a = " + pri_a);

System.out.println("pro_a "+ pro_a);

System.out.println("pub_a =” + pub_a);

}

}

 

The above file PCKG1_ClassTwo belongs to package pckg1, and extends PCKG1_ClassOne, which belongs to the same package.

//PCKG1_ClasslnSamePackage

 

package pckg1;

class PCKG1_ClassInSamePackage {

PCKG1_ClassInSamePackage() {

PCKG1_ClassOne co = new PCKG1_ClassOne();

System.out.println("same package class constructor called");

System.out.println("a = " + co.a);

II accessible in same class only

II System.out.println("pri_a = " + co.pri_a);

System.out.println("pro_a "+ co.pro_a);

System.out.println("pub_a = " + co.pub_a);

}

}

 

The above file PCKG1_ClassInSamePackage belongs to package pckg1, and having an instance of PCKG1_ClassOne.

 

package PCKG1;

//Demo package PCKG1

public class DemoPackage1 {

public static void main(String ar[]) {

PCKG1_ClassOne obl = new PCKG1_ClassOne();

PCKG1_ClassTwo ob2 = new PCKG1_ClassTwo();

PCKG1_ClassInSamePackage ob3 = new PCKG1_ClassxnSamePackage();

}

}

 

The above file DemoPackageI belongs to package pckgI, and having an instance of all classes in pckg1.

 

package pckg2;

class PCKG2_ClassOne extends PCKG1.PCKG1_ClassOne {

PCKG2_ClassOne() {

System.out.println("derived class of other package constructor

called");

II accessible in same class or same package only

II System.out.println("a = " + a);

II accessible in same class only

II System.out.println("pri_a = " + pri_a);

System.out.println("pro_a = " + pro_a);

System.out.println("pub_a = " + pub_a);

}

}

The above file PCKG2_ClassOne belongs to package pckg2. extends PCKG I_ClassOne, which belongs to PCKG1, and it is trying to access data members of the class PCKGI_ClassOne.

 

IIPCKG2_ ClassInOtherPackage

 

package pckg2;

class PCKG2_ClassInOtherPackage {

PCKG2_ClassInOtherpackage() {

PCKG1.PCKG1_ClassOne co = new PCKG1.PCKG1_ClassOne();

System.out.println("other package constructor");

II accessible in same class or same package only

II System.out.println("a ,= " + co.a);

II accessible in same class only

II System.out.println("pri_a = " + co.pri_a);

II accessible in same class, subclass of same or other package

II System.out.println("pro_a = " + co.pro_a);

System.out.println("pub_a = " + co.pub_a);

}

}

 

The above file PCKG2_ClassInOtherPackage belongs to package pckg2, and having an instance of PCKG LClassOne of package pckg I, trying to access its some data members.

 

II Demo package pckg2.


package pckg2;

public class DemoPackage2 {

public static void main(String ar[]) {

PCKG2_ClassOne obl = new PCKG2_ClassOne();

PCKG2_ClassInOtherPackage ob2 = new PCKG2_ClassInOtherPackage();

}

}

 

The above file DemoPackage2 belongs to package pckg2, and having an instance of all classes of pckg2 .