Access modifiers in java

Let’s learn access modifiers in java.

Access modifiers in java

Java access modifiers control the visibility or access of Class variables, class, constructor and methods.

Also read – system array copy

In my previous posts you have seen the use of public, private and protected in few java programs. These are called as access modifiers.

In java we have two types of access modifers; access modifiers and non-access modifiers in java.

Non-access modifiers are final, static, abstract, strictfp, native, synchronized, transient and volatile.

There are four types of access modifiers they are private, protected, public and default.

Let’s understand each access modifiers in java by a simple example.

private access modifier

If a class or class member is “private”, then it can be accessed / visible only within the same class. It cannot be accessed outside the class.

Also read – class and objects in java

Private access modifiers are restrictive one. Because it restricts the access of variables and methods outside the package.

Class and interface cannot be declared as “private”. A class having private constructor can’t create object of that class from outside the class.

class Demo
{
   private void displayName()
   {
      System.out.println("Flower Brackets");
   }
}

public class PrivateDemo 
{
   public static void main(String[] args) 
   {
      Demo obj = new Demo();
      obj.displayName();
   }
}



Output:

access modifiers in java

public access modifier

In this case if a class or class member is “public” it can be accessed globally or from any other class or from any package or from entire java program.

If we want to access a class which is “public” outside the package then we can “import” that class to particular package.

package a1;

public class Example1
{
   public void printName()
   {
      System.out.println("Flower Brackets");
   }
}

package a2;
import a1.*;

public class PublicDemo 
{
   public static void main(String[] args) 
   {
      Example1 obj = new Example1();
      obj.printName();
   }
}



Output:

Flower Brackets


protected access modifier

Now in this case if a class member or class variables is “protected” then it can be accessed within the same package and only to subclasses.

Only difference between protected and default access modifier is that protected access modifier has visibility in subclasses.

package abc;
class Demo
{
   protected void printName()
   {
      System.out.println("Flower Brackets");
   }
}

package def;
import abc.*;

class ProtectedExample extends Demo
{
   public static void main (String args[])
   {
      ProtectedExample obj = new ProtectedExample();
      obj.printName();
   }
}



Output:

Flower Brackets


default access modifier in java

Here in this case if a class member with no access modifier specified then it can be treated with “default” access modifier.

Class members with default access modifier cannot be accessed from outside the package.

Class members, class or method having default access modifier are accessible within same package only.

// java default access modifier
package abc;
class Demo
{
   void printName()
   {
      System.out.println("Flower Brackets");
   }
}

package def;
import abc.*;

class DefaultExample
{
   public static void main (String args[])
   {
      Demo obj = new Demo();
      obj.printName();
   }
}



Output:

Compile error

In the above example class Demo and its method printName() has default access modifier so we cannot access from outside the package.


Here’s the relationship of above discussed access modifiers in java,

access modifiers in java

(Least accessed) private <<< default <<< protected <<< public (most accessed).