Access Mechanism in Packages Demystified

  • 0
     Packages in java are both, a naming and a visibility control mechanism,. In this article, I will try to show the behaviour of access modifiers in different situations. We have 4 type of access in Java, they are public, private, protected and default. Out of these, only public and default can be used with classes. When no access modifier is explicitly written before a member, is is supposed to be in default access.
     The first point to be noted is that once we define a class public, then in the source file, it should be only one class tagged public as well as the source file must have the same name as public class. Public access allow the member to be accessed from anywhere. Default allows the access from within the package. Protected allows access from outside the package but to those only who extend the class. Private restricts the access to only members of the class only. The first source file does not has a main() method and it is to be named as PubClass.java .
package AccessIllustration;

public class PubClass
{
 int n = 1;       //It has default access.
 private int pri_int = 2;   //It has private access.
 protected int pro_int = 3;   //It has protected access.
 public int pub_int = 4;    //It has public access.
 
 public PubClass()
 {
  System.out.println("Currently in the PubClass");
  System.out.println("Value of n is " + n);
  System.out.println("Value of pri_int is" + pri_int);
  System.out.println("Value of pro_int is " + pro_int);
  System.out.println("Value of pub_int is " + pub_int);
 }
}
I have made use of comments to show the details. Second source file would be in same package but would not extend it and through comments, we would see which of the four member variables are accessible along with their reasons. It is to named PubClassSamePackage.java
package AccessIllustration;

class PubClassSamePackage
{
 PubClassSamePackage()
 {
  PubClass pc = new PubClass();
  
  System.out.println("PubClassSamePackage Constructor ");
  System.out.println("Value of n is " + pc.n);    //Default Acess, would work for same package
  //System.out.println("Value of pri_int is" + pc.pri_int);     //Private Access, would not work
  System.out.println("Value of pro_int is " + pc.pro_int); //Same package, would work
  System.out.println("Value of pub_int is " + pc.pub_int); //Public Access, would work
 }

 public static void main(String args[])
 {
  PubClassSamePackage pcs = new PubClassSamePackage();
 }
}
The third source file is to named PubClassDerivedSamePackage.java, as you can see from the name, this class belongs to same package and inherits PubClass class.
package AccessIllustration;

class PubClassDerivedSamePackage extends PubClass
{
 PubClassDerivedSamePackage()
 {
  
  System.out.println("PubDerivedClassSamePackage Constructor ");
  System.out.println("Value of n is " + n);    //Default Acess, would work for same package
  //System.out.println("Value of pri_int is" + pri_int);     //Private Access, would not work
  System.out.println("Value of pro_int is " + pro_int); //Same package, would work
  System.out.println("Value of pub_int is " + pub_int); //Public Access, would work
 }

 public static void main(String args[])
 {
  PubClassDerivedSamePackage pcs = new PubClassDerivedSamePackage();
 }
}
Till now, we were working with the same package, now it is time to move to a different package. The next source file would be named as PubAccess.java, it extends the PUbCLass.
package AccesControl;

class PubAccess extends AccessIllustration.PubClass
{
 PubAccess()
 {
  System.out.println("In constuctor of PubAccess outside package");
  //System.out.println("Value of n is " + n);    //Default Acess, would not work for different package
  //System.out.println("Value of pri_int is" + pri_int); //Private Access, would not work
  System.out.println("Value of pro_int is " + pro_int); //Inherited, would work
  System.out.println("Value of pub_int is " + pub_int); //Public Access, would work
 }
 
 public static void main(String args[])
 {
  PubAccess pa = new PubAccess();
 }
}
Default access forces to work in the same package, outside the package, it does not works. Now, the last source file does not extends the PubClass, instead tries to create an object of PubClass, and then tries to access each variable. It is to be named as PubAccessNotExtend.java
package AccesControl;
package AccessControl;

class PubAccesNotExtend
{
 PubAccesNotExtend()
 {
  AccessIllustration.PubClass pc = new AccessIllustration.PubClass();
  //System.out.println("Value of n is " + pc.n);    //Default Acess, would not work for different package
  //System.out.println("Value of pri_int is" + pc.pri_int); //Private Access, would not work
  //System.out.println("Value of pro_int is " + pc.pro_int); //Different package, not inherited, would not work
  System.out.println("Value of pub_int is " + pc.pub_int); //Public Access, would work
 }
 
 public static void main(String args[])
 {
  PubAccesNotExtend pc = new PubAccesNotExtend();
 }
}

No comments:

Post a Comment