Access modifiers in java

Let’s learn access modifiers in java.

Access modifiers in java

Access modifiers define the limit or scope of a variable, constructor, class or a method. There are two types of modifiers in java; access modifiers and non-access modifiers in java.

There are four types of access modifiers in java; private access modifier, protected access modifier, public access modifier and default access modifier.

private access modifier

Variable or method declared as “private” can be accessed only within the same class. It cannot be accessed outside the class of same package.

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

NOTE:

class and interface cannot be declared as private.

private access modifier example

class Demo
{
   private void display()
   {
      System.out.println("Hello world java");
   }
}
public class PrivateAccessModifierExample
{
   public static void main(String[] args)
   {
      Demo obj = new Demo();
      System.out.println(obj.display());
   }
}


Output:

Compile-time error: The method display() from the type Demo is not visible.


public access modifier

class or method or variable declared as “public” can be accessed globally or everywhere. If we want to access a class which is “public” outside the package then we can “import” that class to particular package.

public access modifier example

package a1;
public class Example1
{
   public void printName()
   {
      System.out.println("Flower Brackets");
   }
}
package a2;
import a1.*;
public class PublicAccessModifierDemo
{
   public static void main(String[] args)
   {
      Example1 obj = new Example1();
      obj.printName();
   }
}


Output:

Flower Brackets


protected access modifier

Data members or methods or variables declared as “protected” can be accessed within same package or subclasses in other package.

A class cannot be protected. protected access modifier in java can be applied on constructor, data members and methods.

protected access modifier example

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

A data member, method or class specified with no access modifier is treated as default access modifier by default.

Data members, methods or variables having default access modifier can be accessed only within the package.

default access modifier example

package abc;
class Demo
{
   void printName()
   {
      System.out.println("Flower Brackets");
   }
}
package def;
import abc.*;
class DefaultExample
{
   public static void main (String args[])
   {
      // since class Demo is not public we cannot access from outside the package
      Demo obj = new Demo();
      obj.printName();
   }
}


Output:

Compile-time error


Here’s the priority of access modifiers in java.

access modifiers in java

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


Also read – HashSet in java