Access Modifiers in JAVA

Access Modifiers are used to Control the access and to maintain the confidentiality of the classes and it's members. There are two access levels in JAVA where access modifiers are applicable. namely,
  1. class level
  2. member level
In Java we have four access modifiers. namely,
  1. Public
  2. Protected
  3. Default
  4. Private

Class Level


Only Public and Default access modifiers are applicable for class level declaration.


public

This can be access from anywhere either from the inside of the package or the outside of the package. If a public class needed to be accessed from the outside of the package, it is a must to import that package which contains that class.



package test;

import access_modifiers.PublicClass;

TestPublic{
    PublicClass pc = new PublicClass();
}

package access_modifiers;

public class PublicClass{

}


We are not using the import statement when accessing a class inside the same package.


default

There is no keyword for the default access modifier. So if a class is not using access modifiers then that class is using the default access modifier which only allows classes to access inside the same package.



package test;

//DefaultClass is not visible to package test
//import access_modifiers.DefaultClass;

TestDefaultOutside{
   // DefaultClass dc = new DefaultClass();
}

package access_modifiers;

class DefaultClass{

}

class TestDefaultInside{
    //DefaultClass is visible to TestDefault which are in the same package
    DefaultClass dc = new DefaultClass();
}


Member Level


All four access modifiers are applicable for member level declarations.

Class Members:
  • Class Variables/ Instance Variables
  • Class Methods/ Instance Methods
Access modifiers are not applicable for local variables since they are in the method scope

Class level access must be given in order to give member level access. as example, members declared as public cannot be accessed outside of the package if the class is declared as default


public

Members declared as public are visible for both inside and outside of the packages.


package test;

import access_modifiers.PublicClass;

TestPublic{
    PublicClass pc = new PublicClass();
    //public members are visible to anywhere where the class is visible
    pc.publicVariable="Can see you";
}

package access_modifiers;

public class PublicClass{
    public String publicVariable;
}



default

Since the default access modifier doesn't have a keyword, any member declared without any access modifier gain the default access which will be only visible to the classes inside the same package

Example 01


package test;

//Public Class is visible to the other packager
import access_modifiers.DefaultClass;

TestDefaultOutside{
   //The default constructor have the same access modifier declared in the class
   DefaultClass dc = new DefaultClass();

   //defaultVariable is not visible since it is declared with a default access modifier
   //pc.defaultVariable="Can't see you";
 
}

package access_modifiers;

class PublicClass{
    String defaultVariable;
}
class TestDefaultInside{
    PublicClass pc = new PublicClass();

    //Can be visible inside the same package
    pc.defaultVariable="Can see you";
}


Example 02


package test;

//DefaultClass is not visible to package test
//import access_modifiers.DefaultClass;
TestPublic{
  // default constructor of a class declared with default access modifier is not visible to other packages
   // DefaultClass dc = new DefaultClass();
   // even though the variable is declared as public since the class is default, the variable is not visible
   // dc.publicVariable="can't see you";
}

package access_modifiers;

class DefaultClass{
     public String publicVariable;
}
class TestDefault{
    //DefaultClass is visible to TestDefault which are in the same package
    DefaultClass dc = new DefaultClass();
}



protected

A member marked as protected is visible to the classes from the same package as the default access modifier. A protected member can be visible to a class from the outside of the package if and only if that class is a subclass of the class protected member belongs-to.  And that protected member can be accessed only via the inheritance mode and not by instantiating an object of it.



package test;

import access_modifiers.PublicClass;

TestProtectedOutside extends PublicClass{
   Public Class is visible to the classes outside of the package
   PublicClass pc = new PublicClass();
   // the protected members are not visible to outside of the package.
   // even if the class is a sub class you can't access it through objects.
   // dc.protectedVariable="can't see you";
   // protected members are visible when it is accessed through inheritance
   super.protectedVariable="can see you";
}

package access_modifiers;

public class PublicClass{
     protected String protectedVariable;
}
class TestProtected{
    //PublicClass is visible to TestProtected
    DefaultClass dc = new DefaultClass();
    //protectedVariable is visible to the classes inside the package
    dc.protectedVariable="can see you";
}



private

Members marked as private can be accessed only within the scope of the class.



package access_modifiers;

public class PublicClass{
    private String privateVariable;
    void setPrivateVariable(){
         //visible only within the class scope
         privateVariable="can see you";
    }
}

TestPrivate{
    PublicClass pc = new PublicClass();
    //Not visible to other classes even though they are in the same package
    pc.privateVariable="can't see you";
}



Summary

public      - outside package,outside package via inheritance, inside package, inside class
protected - outside package via inheritance, inside package, inside class
default     - inside package, inside class
private     - inside class



Comments

Popular posts from this blog

Flow Charts

ධර්මය, ආක්‍රමණය හා යුද්ධය

ආගමක් නැති මනුස්සයෙක්