Saturday 25 May 2013

JAVA'S ACCESS CONTROL MECHANISM THROUGH ACCESS SPECIFIERS

This post will give you a detailed explanation regarding Java's access control mechanism and access specifiers, when packages are involved. By this post you will also be able to implement packages, and learn all the concepts of packages. Java provides three access specifiers:-
  1. public 
  2. private, and 
  3. protected.


A brief description about packages:-
Packages can be thought as a container for classes and other sub-ordinate packages that are basically used to keep class name-space compartmentalized.

Rules regarding access control mechanism:-
  1. Data members or member functions declared with public key-word can be accessed within any other class regardless of the package in which that class is.
  2. The members declared with private key-word, can strictly be accessed only by the members of that class. They can't be accessed by any other class, even when the class is defined in the same package.
  3. The protected key-word is used specially in those cases, when you are working with more than one packages and at the same time inheritance is also involved. Members declared with protected key-word are accessible by all the classes within the current package, but for classes in the other package, these members will be accessible only to those classes that are sub-class(which contain the members that are declared protected), the classes that are not sub-class can't access these members.
  4. In case if you have not mentioned any access specifier, then these members can be accessed by any class within the current package, but it can't be accessed within any class of different package. Note that inheritance has nothing to do with this case.
The following table clearly illustrate the above mentioned concepts.


A brief description about the code:-
There are two packages:-
  1. p1, and 
  2. p2
Package p1 consists of four different classes:-
  • protection
  • derived 
  • same_package
  • test1
Package p2 consists of three classes:-
  • protection2
  • other_package
  • test2
derived(of package p1) and protection2(of package p2) class are the child class of protection class(of package p1).



SOURCE CODE:-

/*******************************************************************************/
/*file name ---> protection.java*/

package p1;

public class protection {
int n=1;
private int n_pri=2;
protected int n_pro=3;
public int n_pub=4;
/* public constructor */
public protection() 
{
System.out.println("default constructor of protection starts here");
System.out.println("n = "+n);
System.out.println("n_pri = "+n_pri);
System.out.println("n_pro = "+n_pro);
System.out.println("n_pub = "+n_pub);
System.out.println("default constructor of protection ends here");
System.out.println();
}
}



/*******************************************************************************/
/*file name ---> derived.java*/

package p1;

public class derived extends protection{
derived()
{
System.out.println("default constructor of derived starts here");
System.out.println("n = "+n);
/* System.out.println("n_pri = "+n_pri);
* the above statement will generate an error
* n_pri is declared private so it is not accessible in other classes
*/
System.out.println("n_pro = "+n_pro);
System.out.println("n_pub = "+n_pub);
System.out.println("default constructor of derived ends here");
System.out.println();
}
}



/********************************************************************************/
/*file name ---> same_package.java*/

package p1;

public class same_package {
same_package()
{
protection p=new protection();
System.out.println("default constructor of derived starts here");
System.out.println("p.n = "+p.n);
/* System.out.println("p.n_pri = "+p.n_pri);
* the above statement will generate an error
* p.n_pri is declared private so it is not accessible in other classes
*/
System.out.println("p.n_pro = "+p.n_pro);
System.out.println("p.n_pub = "+p.n_pub);
System.out.println("default constructor of derived ends here");
System.out.println();
}
}



/******************************************************************************/
/*file name ---> protection2.java*/

package p2;

public class protection2 extends p1.protection{
public protection2()
{
System.out.println("default constructor of protection2 starts here");
/* System.out.println("n = "+n);
* the above statement will generate an error
* as n is declared with no access specifier in protection class
* so n is not accessible to a class that is not of the same package as protection is. 
*/
/* System.out.println("n_pri = "+n_pri);
* the above statement will generate an error
* n_pri is declared private so it is not accessible in other classes
*/
/*
* protected data types of the base class are accessible 
* even in subclass of different packages.
*/
System.out.println("n_pro = "+n_pro);
/*
* n_pub is explicitely specified as public
* so can be accessed by any of the class that is even from different package.
*/
System.out.println("n_pub = "+n_pub);
System.out.println("default constructor of protection2 ends here");
System.out.println();
}
}



/*****************************************************************************/
/*file name ---> other_package.java*/

package p2;

public class other_package {
public other_package()
{
p1.protection obj=new p1.protection();
System.out.println("default constructor of other_package starts here");
/* System.out.println("obj.n = "+obj.n);
* the above statement will generate an error
* as n is declared with no access specifier in protection class
* so n is not accessible to a class that is not of the same package as protection is. 
*/
/* System.out.println("obj.n_pri = "+obj.n_pri);
* the above statement will generate an error
* n_pri is declared private so it is not accessible in other classes
*/
/* System.out.println("obj.n_pro = "+obj.n_pro);
* protected data types of the base class are accessible 
* only in subclass of different packages.
* they are not accessible in classes that are not sub-classes
*/
/*
* obj.n_pub is explicitely specified as public
* so can be accessed by any of the class that is even from different package.
*/
System.out.println("obj.n_pub = "+obj.n_pub);
System.out.println("default constructor of protection2 ends here");
System.out.println();
}

}



/*******************************************************************************/
/*file name ---> test1.java*/

package p1;

public class test1 {
public static void main(String[] args) {
System.out.println("you are in the file test1.java");
protection obj1=new protection();
derived obj2=new derived();
same_package obj3=new same_package();
}
}



/********************************************************************************/
/*file name ---> test2.java*/

package p2;

public class test2 {
public static void main(String[] args) {
System.out.println("you are in the file test2.java");
protection2 obj1=new protection2();
other_package obj2=new other_package();
}
}
/******************************************************************************/


Output
output of test1.java :-

you are in the file test1.java
default constructor of protection starts here
n = 1
n_pri = 2
n_pro = 3
n_pub = 4
default constructor of protection ends here

default constructor of protection starts here
n = 1
n_pri = 2
n_pro = 3
n_pub = 4
default constructor of protection ends here

default constructor of derived starts here
n = 1
n_pro = 3
n_pub = 4
default constructor of derived ends here

default constructor of protection starts here
n = 1
n_pri = 2
n_pro = 3
n_pub = 4
default constructor of protection ends here

default constructor of derived starts here
p.n = 1
p.n_pro = 3
p.n_pub = 4
default constructor of derived ends here

output of test2.java

you are in the file test2.java
default constructor of protection starts here
n = 1
n_pri = 2
n_pro = 3
n_pub = 4
default constructor of protection ends here

default constructor of protection2 starts here
n_pro = 3
n_pub = 4
default constructor of protection2 ends here

default constructor of protection starts here
n = 1
n_pri = 2
n_pro = 3
n_pub = 4
default constructor of protection ends here

default constructor of other_package starts here
obj.n_pub = 4
default constructor of protection2 ends here


No comments:

Post a Comment