Access Modifiers in Java

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 variable, method, constructor or class. We can change the access level of variables, methods, constructors, class 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 private variable and 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 member and private method. 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, 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");
	}

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

	public static void main(String[] args) {

		Demo_1 obj1 = new Demo_1();

	}
}

Output
Private Constructor

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();
	}

}

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

Default

If we don’t use any modifier, it is treated as default access modifier. The default modifier is accessible only within 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() { // default constructor

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

	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();
	}

}

Output
Default Constructor
Value of X :10
Display default method

Protected

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

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

In the below example, I have created the 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. But variable, constructor and method of this package is 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() { // protected constructor

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

	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();

	}

}

Output
Protected Constructor
Value of X :10
Display protected method

Public

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

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

package Parent_A;

public class Demo_1 {

	public int x = 10; // public variable

	public Demo_1() { // public constructor

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

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

}

package Parent_B;

import Parent_A.Demo_1;

public class Demo_3 {

	public static void main(String[] args) {

		Demo_1 obj1 = new Demo_1();

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

	}

}

Output
Public Constructor
Value of X :10
Display public method

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s