Java Access Modifiers: Explained with Examples

HOME

There are two types of access modifiers in Java – access modifiers and non-access modifiers.

The access modifier of Java specifies the scope of the variable, method, constructor, or class. We can change the access level of variables, methods, constructors, and classes by applying access modifiers to them.

There are 4 types of access modifiers.

  1. Private: The code is only accessible within the declared class. It cannot be accessed from outside the class.
  2. Default: The code is only accessible in the same package. It cannot be accessed from outside the package. If you do not specify any access level, it will be the default.
  3. Protected: The code is accessible in the same package and subclasses. If you do not make the child class, it cannot be accessed from outside the package.
  4. Public: The class is accessible by any other class. It can be accessed from within the class, outside the class, within the package, and outside the package.

Java Access Modifiers

Private

The private access modifier is accessible only within the class.

In this example, I have created one class – Demo_1 in package Parent_A. It contains a private variable and a private method. We can access private variables and methods within the class.

package Parent_A;

public class Demo_1 {

	private int x = 10; // private variable

	private void display() { // private method
		System.out.println("Display private method");
	}

	public static void main(String[] args) {

		Demo_1 obj1 = new Demo_1();

		System.out.println("Value of X :" + obj1.x);
		obj1.display();
	}

}

In the below example, we have 2 classes – Demo_1 and Demo_2. Class Demo_1 contains private data members and private methods. We are accessing these private members from outside the class, another class Demo_2 so there is a compile-time error.

package Parent_A;

public class Demo_1 {

	private int x = 10; // private variable

	private void display() { // private method
		System.out.println("Display private method");
	}

}
package Parent_A;

public class Demo_2 {

	public static void main(String[] args) {

		Demo_1 obj1 = new Demo_1();
		System.out.println("Value of X :" + obj1.x);
		obj1.display();
	}

}

Private Constructor

In the below example, the private constructor is accessible within the class.

package Parent_A;

public class Demo_1 {
    private int x = 10; // private variable

    private Demo_1() {

        System.out.println("Private Constructor: Value of x : " + x);
    }

    private void display() { // private method
        System.out.println("Display private method");
    }

    public static void main(String[] args) {

        Demo_1 obj1 = new Demo_1();

    }
}

If you make any class constructor private, you cannot create the instance of that class from outside the class.

package Parent_A;

public class Demo_1 {

	private int x = 10; // private variable

	private Demo_1() {

		System.out.println("Private Constructor");
	}

	private void display() { // private method
		System.out.println("Display private method");
	}

}

package Parent_A;

public class Demo_2 {

	public static void main(String[] args) {

		Demo_1 obj1 = new Demo_1();
	}

}

Default

If we don’t use any modifier, it is treated as a default access modifier. The default modifier is accessible only within the package. It cannot be accessed from outside the package. It provides more accessibility than private. But, it is more restrictive than protected and public.

In this example, we have created two classes – Demo_1 and Demo_2 in package – Parent_A. We are accessing the class Demo_2 from within its package, since it is default, so it can be accessed from within the package.

package Parent_A;

public class Demo_1 {

    int x = 10; // default variable

    Demo_1() {

        System.out.println("Default Constructor: Value of x : " + x);
    }

    void display() { // default method
        System.out.println("Display default method");
    }

}

package Parent_A;

public class Demo_2 {

	public static void main(String[] args) {

		Demo_1 obj1 = new Demo_1();
		System.out.println("Value of X :" + obj1.x);
		obj1.display();
	}

}

Protected

The protected access modifier is accessible within the package and outside the package but through inheritance only.

The protected access modifier can be applied to the data member, method, and constructor. It can’t be applied to the class.

In the below example, I have created two packages Parent_A and Parent_B. The class Demo_1 of Parent_A package is public, so can be accessed from outside the package. However the variable, constructor, and method of this package are declared as protected, so it can be accessed from outside the class only through inheritance.

package Parent_A;

public class Demo_1 {
    
    protected  int x = 10; // protected variable

    protected Demo_1() {

        System.out.println("Protected Constructor: Value of x : " + x);
    }

    protected void display() { // protected method
        System.out.println("Display Protected method");
    }

}

package Parent_B;

import Parent_A.Demo_1;

public class Demo_3 extends Demo_1{

    public static void main(String[] args) {

        Demo_3 obj1 = new Demo_3();
        System.out.println("Value of X :" + obj1.x);
        obj1.display();
    }

}

Public

The public access modifier is accessible everywhere. It has the widest scope among all other modifiers.

In the below example, I have created two packages Parent_A and Parent_B. The variable, constructor, and method of this package are declared as public, so it can be accessed from outside the class or outside the package.

package Parent_A;

public class Demo_1 {
    public   int x = 10; // public variable

    public  Demo_1() {

        System.out.println("Public Constructor: Value of x : " + x);
    }

    public  void display() { // public method
        System.out.println("Display Public method");
    }

}

package Parent_B;

import Parent_A.Demo_1;

public class Demo_4 {

    public static void main(String[] args) {

        Demo_1 obj1 = new Demo_1();

        System.out.println("Value of X :" + obj1.x);
        obj1.display();

    }

}

Leave a comment