Java : Access Modifiers/Specifiers


Java access modifiers helps to restrict the scope of a class, constructor , variable , method or data member. There are four types of access modifiers available in java:

  • default (No Keyword Required)
  • private
  • protected
  • public

Note : Some people also called as access specifiers.

Points to remember :

  • Try to use most restrictive access level that make sense for member
  • Try to use private as long as not having reason to not use.
  • Use constants value as public.

Scope of Access Modifiers

Below are the scope of each access modifiers:

default private protected public
Same Class Yes Yes Yes Yes
Same Package Subclass Yes No Yes Yes
Same Package non Sub Class Yes No Yes Yes
Different Package Sub Class No No Yes Yes
Different Package Non Sub Class No No No Yes

Below are in-depth description about each access modifiers with example.

Default

When no access modifier is specified for a class , method or data member. It is considered as having the default access modifier by default.

  • Default access modifiers are accessible only within the same package.

Example
In this example, created two packages (pkg1 and pkg2) and the respective classes (TestClass1 and TestClass2) which are not having access modifiers. By this example trying to access TestClass1 from pkg1 to another pkg2 that’s not allowed thats why through compile time issue.

//Java program to illustrate default modifier
package pkg1; 

//TestClass1 is having Default access modifier
class TestClass1
{
    void display()
       {
           System.out.println("Welcome to Facing Issues On IT!");
       }
}
//Java program to illustrate error while
//using class from different package with
//default modifier
package pkg2;
import pkg1.*; 

//This class is having default access modifier
class TestClass2
{
    public static void main(String args[])
       {
          //accessing class TestClass1 from package pkg1
          TestClass1 obj = new TestClass1(); 

          obj.display();
       }
}

Output


Compile time error

private

The private keyword is used in the declaration of a method, field, or inner class; private members can only be accessed by other members of their own class in which they are declared.

  • private means “only visible within the enclosing class”.
  • Any other class of same package will not be able to access these private members.
  • private can be applied on nested classes but not on top level Classes or interface.

Example
In this example, created class TestClass1 in package pkg1 where method display having access modifiers as private. This method is trying to access in TestClass2 but due to private restriction not allowed to access out side of class thats why through compile time issue.

//Java program to illustrate error while
//using class from different package with
//private modifier
package pkg1; 

class TestClass1
{
   private void display()
    {
         System.out.println("Welcome to Facing Issues On IT!");
    }
}
class TestClass2
{
   public static void main(String args[])
      {
          TestClass1 obj = new TestClass1();
          //trying to access private method of another class
          obj.display();
      }
}

Output


error: display() has private access in A
        obj.display();

protected

The protected keyword is used in the declaration of a method, field, or inner class; protected members can only be accessed by members of their own class, that class’s subclasses or classes from the same package.

  • protected means “only visible within the enclosing class and any subclasses”
  • The methods or data members declared as protected are accessible within same package or sub classes in different package.

Example
In this example, created two packages (pkg1 and pkg2) and the respective classes (TestClass1 and TestClass2) which are having access modifiers as public. Class TestClass1 is having method display with protected access modifiers. Here we are extending TestClass2 with super class TestClass1 and accessing method display()of super class.

//Java program to illustrate
//protected modifier
package pkg1; 

//Class TestClass1
public class TestClass1
{
   protected void display()
    {
        System.out.println("Welcome to Facing Issues On IT!");
    }
}
//Java program to illustrate
//protected modifier
package pkg2;
//importing all classes in package pkg1
import pkg1.*; 

//Class TestClass2 is subclass of TestClass1
class TestClass2 extends TestClass1
{
   public static void main(String args[])
   {
       TestClass2 obj = new TestClass2();
       obj.display();
   }
}

Output:


Welcome to Facing Issues On IT!

public

The public keyword is used in the declaration of a class, method, or field; public classes, methods, and fields can be accessed by the members of any class.

  • There is no restriction on the scope of a public data members.
  • The public access modifier has the widest scope among all other access modifiers.

Example
In this example, created two packages (pkg1 and pkg2) and the respective classes (TestClass1 and TestClass2) which are having access modifiers public. By this example trying to access TestClass1 method display() having public access from pkg1 to another package pkg2 class TestClass2 .

//Java program to illustrate
//public modifier
package pkg1;
public class TestClass1
{
   public void display()
      {
         System.out.println("Welcome to Facing Issues On IT!");
      }
}
package pkg2;
import pkg1.*;
class TestClass2
{
    public static void main(String args[])
      {
          TestClass1 obj = new TestClass1();
          obj.display();
      }
}

Output


Welcome to Facing Issues On IT!
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s