Access Modifiers in Java

Article by: Manish Methani

Last Updated: October 27, 2021 at 10:04am IST
7 min 47 sec read

What is Java Class? Why is it being used in Object-Oriented programming language? What is Object-Oriented programming? 
Do you play games? Yes, a game like Super Mario, Pacman, breakout game, snake game, etc. These different types of games are part of one big blueprint named "GAME" :). So there is something that creates a blueprint of various types of games and keeps them together in a player-like video games device (eg, Play Station, etc).

What is Class?

As I explained in the above example, a Class is just a blueprint that contains stuff like properties, variables, member functions, and all. A class definition starts with class keyword followed by the class name and class body is enclosed by pair of parenthesis

Syntax :-

modifier class
{

}

The modifier in the given syntax is called an access modifier in java. Let's have a look at the difference between public, private, protected access modifiers in Java.

Access Modifiers in Java

There are various types of Access Modifiers in Java as follows:-

1) public

2) private

3) protected

1) public access:-

When a method or variable member is declared public, it means all other classes, regardless of the package they belong to, can access the member. In simple language, you can assume the packages in Java as the folders in the computer system with some permissions.

Example:-

Assume there are two different packages (Packages in Java are nothing but folders in simple language) named aPackage & bPackage. aPackage contains one class & bPackage contains one class.

package aPackage;
import bPackage.*;      // Import all classes in the bPackage package
class SuperClass
{
  public static void main(String[] args)
  {
  SubClass sub = new SubClass();
  sub.display();
  } 
}

Now look at the second file:

package bPackage;
public class SubClass
{
  public void display()
  {
    System.out.println("This is subclass"); 
  }
}

Output :-

This is subclass

Here, there are two packages (Consider Packages as folders always) named as aPackage & bPackage . aPackage contains SuperClass.java & bPackage contains SubClass.java Both the classes have public access.

In the first file Superclass.java, we created an instance of Subclass and called its display() method and since that display method was public inside Subclass that's why the method is visible in Superclass. And as you see both the files were in two different folders (Packages) public access allows using its methods from different classes too.

2) private access:-

When a method or variable member is declared private, no other class, no other packages can access the private properties except the class in which those private members or methods are written. This access modifier is called a private access modifier.

Example :-

Assume there are two different packages (Packages in Java are nothing but folders in simple language) named aPackage & bPackage. aPackage contains one class & bPackage contains one class.

package aPackage;
import bPackage.*;      // Import all classes in the bPackage package
class SuperClass
{
  public static void main(String[] args)
  {
  SubClass sub = new SubClass();
  sub.display();        // Compilation error at this line 
  } 
}

Now look at the second file:

package bPackage;
public class SubClass
{
  private void display()    
  {
    System.out.println("This is subclass"); 
  }
}

As you see display() method in SubClass is private . So in SuperClass when it tries to access display method, compilation error appears because you cannot access private methods. But SubClass is public thats why you can access it but not its methods . If you make SubClass modifier also private , then

package aPackage;
import bPackage.*;      // Import all classes in the bPackage package
class SuperClass
{
  public static void main(String[] args)
  {
  SubClass sub = new SubClass();   // Compilation error at this line 
  sub.display();        // Compilation error at this line 
  } 
}

Now look at the second file:

package bPackage;
private class SubClass
{
  private void display()    
  {
    System.out.println("This is subclass"); 
  }
}

3) default vs protected Access

default access allows accessing the members & methods only if both classes are in the same folder (package) whereas protected access allows to access the members & methods of the same class, subclass through inheritance irrespective of any package access. Any package classes can use methods but only through inheritance.

Let's discuss the default access modifier in the same package and in a different package. It works in the same package through inheritance but not in another package.

default access in same package

package aPackage;
  class SuperClass {
    void display() {   // No modifier means method has default
                       // access 
      System.out.println("First Class Method");
    }
}
  
 public class SubClass extends SuperClass
  {
    public static void main(String[] args)
    {
   SubClass sub = new SubClass(); 
   sub.display();
    }
  }

Output :-

First Class Method

As you see, the display method in SuperClass contains no access modifier. So access modifier in this case is the default. default modifier will not allow using methods of superclass object of another package but in the same package, it will work fine through inheritance( As you see inside SubClass, we are calling the display() method of SuperClass through Subclassinstance sub)

deafult Access in another package :-

package aPackage;
  public class SuperClass {
    void display() {   // No modifier means method has default
                       // access 
      System.out.println("First Class Method");
    }
}
package bPackage;
import aPackage.*;
class SubClass
{
  static public void main(String[] args)
     {
      SuperClass firstClass = new SuperClass();
      firstClass.display(); // Compilation Error
     } 
}

As you see, the display method in SuperClass contains no access modifier. So access modifier in this case is default. default modifier will not allow using methods of superclass object of another package but in the same package, it will work fine through inheritance. But in this program, both the classes are in different packages. SuperClass is in aPackage and SubClass is in bPackage and if we tried to call display() method of SuperClass in SubClass then compilation error is being shown.

protected access :-

protected access is used to access the methods of same class plus another class in same as well as in another package but only through inheritance.

package aPackage;

public class SuperClass {
   protected void display()
    {
      System.out.println("SuperClass Method");
    }
}
package bPackage;
import aPackage.*;
public class SubClass extends SuperClass{
     public static void main(String []args)
     {
       SubClass superClass = new SubClass();
       superClass.display();
     }
}

Output :-

SuperClass Method.

Watch Video Tutorials at Codzify YouTube Channel:

Codzify Logo

Terms and Conditions    Cookie Policy   Refund Policy   Adsense Disclaimer

Contact: teamcodzify@gmail.com