Access modifiers in java

Let’s learn access modifiers in java.

Access modifiers in java

Access modifiers sets the visibility 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” is only visible within the class it is declared. It is not visible anywhere else including in subclasses of its class.

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” is visible to all classes, whether they are in same package or imported package containing public class.

A public class fields and public method can be accessed from any class anywhere, even in different 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” is visible anywhere in its own package but also in subclasses even if they are in another 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.

access modifiers in java

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


Also read – HashSet in java