Access Modifiers In Java With Examples

Java provides us a many number of access modifiers to set access levels for class, variables, methods and constructor. It means the access modifiers in java specify scope of a data member, method, constructor or a class. The four access modifiers in JAVA are private, default, protected and public.


Table of Contents

Types of Access Modifiers

There are 4 types of java access modifiers:

  • private – accessible within class only
  • default – accessible available to the package only
  • protected – accessible within package and outside the package but through inheritance only
  • public – accessible everywhere

Below we discuss all four access modifiers in detail:


Private Access Modifier:

The private access modifier is accessible only within class. It means the methods, variables and constructors that are declared as private can only be accessed within the declared class itself. It is very restrictive access modifier.

Variables that are declared private can be accessed outside the class if public getter methods are present in the class.

Important Note: We cannot make class and interfaces private.

Program example of private access modifier:

Let us take an example to show the use of private access modifier.

Step 1: First we create class PrivateClass in which we declare one private data member and one private method:

class PrivateClass

{

  private int x= 10;

  private void show()

  {

    System.out.println("Private class");

  }

}

Step 2: Second we create a class PrivateAccess in which we call the PrivateClass class data member and method:

class PrivateAccess

{

  public static void main(String args[])

  {

    PrivateClass obj=new PrivateClass();

    System.out.println(obj.x);  //Compile Time Error

    obj.show();  //Compile Time Error

  }

}

Explanation of Example:

In PrivateAccess class when we are trying to call the private data member and method of a PrivateClass class it gives us compile time error because private data members and methods have a access level to PrivateClass class only.


Default Access Modifier:

If we don’t use any modifier, it is treated as default access modifier by default. In other words we can say it id default if no access modifier for a class, method, field, etc is explicitly declared. The default modifier is accessible only within package.

A variable or method which is declared without any access modifier is available to any other class in the same package.

Program Example of default access modifier

Let us take an example to show the use of default access modifier.

Step 1: First we create a class DefaultClass under a package pack in which we declare default method:

package pack;

class DefaultClass

{

  void show()

  {

    System.out.println("Default class");

  }

}

Step 2: Second we create a class DefaultAccess under a package mypack and imports the above package pack:

package mypack;

import pack.*;

class DefaultAccess

{

  public static void main(String args[])

  {

    DefaultClass obj = new DefaultClass(); //Compile Time Error

    obj.show(); //Compile Time Error

  }

}

Explanation of Example:

In the above example, the scope of class DefaultClass and its method show() is default so it cannot be accessed from outside the package. As a result it shows compile time error.


Protected Access Modifier:

We can access protected access modifier either within package and outside the package but through inheritance only. It means variables, methods and constructors which are declared as protected in a base class can be accessed only by the child classes in other package.

The protected access modifier can be applied on the data member, method and constructor. It cannot be applied on the class and interfaces. Protected access modifier gives a chance to the child class to use the helper method or variable, while preventing from trying to use a class i.e. non related.

Program Example of protected access modifier

Let us take an example to show use of protected access modifier:

Step 1: First we create a class ProtectedClass under a package pack1 in which we declare a protected method show():

package pack1;

class ProtectedClass

{

  protected void show()

  {

    System.out.println("Protected class");

  }

}

Step 2: Second we create a class ProtectedAccess under a package mypack1 and import a package pack1:

package mypack1;

import pack1.*;

class ProtectedAccess extends ProtectedClass

{

  public static void main(String args[])

  {

    ProtectedAccess obj = new ProtectedAccess();

    obj.show();

  }

}

Output:

Protected class

Explanation of Example:

The ProtectedClass class of pack1 package is public, so it can be accessed from outside the package. But show() method of this package is declared as protected, so it can be accessed from outside the class only through inheritance.


Public Access Modifier:

The public access modifier is accessible everywhere. It means a class, method, constructor, interface etc declared as public can be accessed from any other class. It has the widest scope among all other modifiers.

Program Example of public access modifier

Let us take an example to show the use of public access modifier.

Step 1: First we create a class PublicClass in which we declare the public method show():

class PublicClass

{

  public void show()

  {

    System.out.println("Public class");

  }

}

Step 2: Second we create a class PublicAccess in which we call the method of PublicClass class:

class PublicAccess

{

  public static void main(String args[])

  {

    PublicClass obj = new PublicClass();

    obj.show();

  }

}

Output:

Public class

Understanding all access modifiers in java

Let’s understand the access modifiers with the help of following table:

Access Modifier within a class within a package outside a package by subclass only outside a package
Private Yes No No No
Default Yes Yes No No
Protected Yes Yes Yes No
Public Yes Yes Yes Yes

Leave a Reply

Your email address will not be published. Required fields are marked *