Java Access Modifier


JavaViews 2806

Java access modifier allows us to set the visibility or access rights for variables, methods, classes, and constructors. In general, there are 2 different types of access modifiers in Java.

  • Access modifiers – controls the access level
  • Non-access modifiers – does not control the access level but provides other functionalities.

In this tutorial, we will discuss different types of java access modifiers.

Types of Java access modifier

There are 4 different types of java access modifiers.

  • Default: It does not require any keyword and is considered a default. The scope is within the package.
  • Private: Scope is within the class alone.
  • Protected: It is accessible within the package. To access outside the package, we can do it only through inheritance.
  • Public: It is accessible everywhere

Java Access Modifiers

Now let’s understand each type of java access modifiers in detail.

Default access modifier

When we do not specify any modifier for a class, variable, method, or constructor, we consider it a default access modifier. The scope of this type of java access modifier is only within the package and we cannot access them from other classes outside the package. But if another class is within the same package, then we can access them.

In the below example, we have created 2 packages com.Modifiers and com.AccessModifiers where class SimpleClass does not have any access modifier hence it is a default modifier. This means we cannot access any methods or variables of this class from other packages. This is why we go compilation error when we tried to create a class instance of SimpleClass and while accessing the method.

package com.AccessModifiers;

class SimpleClass {

  void display() {
    System.out.println("This is a simple class");
  }
}

 

package com.Modifers;
import com.AccessModifiers.*;

public class DefaultClass {

  public static void main(String[] args) {
    SimpleClass s = new SimpleClass(); //throws error
    s.display(); //throws error

  }

}
Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
  The type SimpleClass is not visible
  The type SimpleClass is not visible
  The type SimpleClass is not visible

  at com.Modifers.DefaultClass.main(DefaultClass.java:7)

However, we can access them within different classes of the same package. For example, we create another class MainClass within the same package com.AccessModifiers. From this class, we can create an instance of SimpleClass and access the method display. This is because both the classes SimpleClass and MainClass are within the same package.

package com.AccessModifiers;

public class MainClass {

  public static void main(String[] args) {
    SimpleClass s = new SimpleClass();
    s.display();

  }

}
This is a simple class

Private access modifier

When we use the keyword private before any variable, method, or constructor, its access is restricted within the same class. If we try to access these private members from a different class, it will throw a compilation error. This type of java access modifier provides more restrictions and security. However, using inheritance and through getter and setter methods, we can access private variables. In this way, we cannot directly change the value of these private variables.

Below is an example of accessing a private method and a private variable outside the class. In such a case, it throws a compilation error as shown below.

package com.AccessModifiers;

class PrivateClass {
  private String value = "Java";
  
  private void display() {
    System.out.println("This is a private method");
  }
  
}
public class PrivateClassDemo {

  public static void main(String[] args) {
    PrivateClassDemo p =new PrivateClassDemo();
    p.display(); //compilation error
    System.out.println(p.value); //compilation error

  }

}
Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
  The method display() is undefined for the type PrivateClassDemo
  value cannot be resolved or is not a field

  at com.AccessModifiers.PrivateClassDemo.main(PrivateClassDemo.java:15)

We can access these private members in the same class as shown below.

package com.AccessModifiers;

public class PrivateClassDemo {
  private String value;
  
  private void display(String v) {
    System.out.println(value);
  }

  public static void main(String[] args) {
    PrivateClassDemo p =new PrivateClassDemo();
    p.value = "Java";
    p.display(p.value);

  }

}
Java

Below is an example of a private constructor. If we create a private constructor, then we cannot create an instance for that constructor. Also, we cannot make a class private unless it is a nested class.

package com.AccessModifiers;

class PrivateClass {
  
  private PrivateClass() {
    System.out.println("This is a private constructor");
  }
}

public class PrivateClassDemo {

  public static void main(String[] args) {
    PrivateClass p = new PrivateClass();

  }

}
Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
  The constructor PrivateClass() is not visible

  at com.AccessModifiers.PrivateClassDemo.main(PrivateClassDemo.java:18)

Protected access modifier

When we use the keyword protected before any variables, methods, or constructors, then the scope of these protected members is within the package only. If we need to access this type of java access modifier from a class outside the package, then it is possible only through inheritance.

A protected access modifier provides more accessibility than the default access modifier.

In the below example, we have created 2 different packages where, in 1 package, we have a protected variable and a protected method. In order to access this from a different class outside the package, we can use inheritance to achieve it. If we try to access protected members without inheritance, we will get a compilation error.

//Package: com.AccessModifiers

package com.AccessModifiers;

public class ProtectedClass {
  protected String value;
  
  protected void display() {
    System.out.println("This is a protected method");
  }
}

//Package: com.Modifiers

package com.Modifers;
import com.AccessModifiers.*;

public class DefaultClass extends ProtectedClass{

  public static void main(String[] args) {
    DefaultClass p = new DefaultClass();
    p.value = "Protected";
    System.out.println("String value: " + p.value);
    p.display();
  }

}
String value: Protected
This is a protected method

Public access modifier

You might be familiar with the public keyword as we have used in most of the java program examples throughout the tutorial. Public access modifier provides full accessibility anywhere in class or package and there is no restriction. This is the most common type of java access modifier that we use generally.

Below is an example of a public class, method, and variable. We have created 2 different packages. Since the variable, method, and class is public, we can easily access them from a different class outside the package even without extending the class.

//Package: com.AccessModifiers

package com.AccessModifiers;

public class PublicClass {
  
  public String value;
  
  public void display() {
    System.out.println("This is a public method");
  }

}

//Package: com.Modifiers

package com.Modifers;
import com.AccessModifiers.*;

public class MainClass {

  public static void main(String[] args) {
    PublicClass p = new PublicClass();
    p.value = "Public";
    System.out.println("String value: " + p.value);
    p.display();

  }

}
String value: Public
This is a public method

Access Modifier in Method Overriding

When we override a method by using a java access modifier, the method in the subclass should not be more restrictive than the parent class method. In other words, we cannot reduce visibility. In this case, it will result in a compilation error. Please refer to our Polymorphism in java tutorial, to understand more about method overriding.

In the below example, in PublicClass, we have created a public method display.

package com.AccessModifiers;

public class PublicClass {
  
  public String value;
  
  public void display() {
    System.out.println("This is a public method");
  }

}

Now, in the MainClass, we have overridden the display method with a default access modifier. Since the default modifier is more restrictive than the public, it throws a compilation error.

package com.Modifers;
import com.AccessModifiers.*;

public class MainClass extends PublicClass{
  
  void display() {
    System.out.println("Default method");
  }

  public static void main(String[] args) {
    MainClass m = new MainClass();
    m.display();

  }

}
Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
  Cannot reduce the visibility of the inherited method from PublicClass

  at com.Modifers.MainClass.display(MainClass.java:6)
  at com.Modifers.MainClass.main(MainClass.java:12)

Reference

Translate »