Java Access Modifiers- public, protected, private and default

Access specifiers also known as Access modifiers are keywords that enable a user to control the access limits of a certain class, method, constructor, inner classes or even variables.

There are four access modifiers in Java:

  • Public
  • Protected
  • Private
  • Default

Privacy and access control is an important aspect of any programming language that limits the usage of any particular entity or its attributes(variables) and features(methods) by the external as well as internal entities.

The figure below shows the visibility of the members of class A in other class within the same package and outside the package. If an access modifier is written inside a package then it means it is available to all the entities of that package.

Access Specifiers


Let’s discuss each access modifier in detail-

The Public Access Modifier

The ‘public‘ access modifier makes the variable, constructor, interface or method accessible in any of the classes. Any class declared as ‘public can have its public members accessed anywhere inside Java. It is the widest scope available in Java. But any java class declared as ‘public‘ still needs to be imported in another package. In order to make any class, method, interface or variable as ‘public‘ we just need to write the ‘public‘ keyword in the declaration statement. The program below shows the use and scope of the ‘public‘ members.

class Demo {
	public int a=5; //giving the variable a public access
	
	/*the method show also has public access*/
	public void show() {
		System.out.println("Inside class Demo");
	}
}

/*Creating another class inside same package to access the members of Demo class*/
public class PublicDemo {

	public static void main(String args[]) {
		Demo ob = new Demo();
		ob.show(); //calling the show() method inside another class within same package
		
		System.out.println("The value of a is : "+ob.a);//The variable a is accessed inside another class within same package
	}
}
		
		

Output:-
Inside class Demo
The value of a is : 5

Question – Do you wonder why the main method is public and static in the program?

Answer – You will witness this in every Java program as this is the entry point for the JVM to launch the program. It is public so that it can be accessed from everywhere and it is static so that it can be called without creating any object.


The Protected Access Modifier

Any member declared as ‘protected‘ can be accessed within any class in the same package and also within any subclass of the class where the member is declared. The subclass can be inside another package as well. Interfaces and classes cannot be declared as ‘protected‘. But methods, constructors, inner classes and variables can be declared ‘protected‘. The members(i.e., methods and variables) inside the interface cannot be declared as ‘protected‘.

class Demo {
	protected int a=5; //giving variable a protected access
}

/*a subclass of the class Demo where a is declared*/
class ProtectedDemo extends Demo {
	protected void display() {
		System.out.println("The value of a is : "+a); //variable a is easily accessible inside the subclass
	}
}

/*Creating another class within the same package*/
public class TestProtected {
	
	public static void main(String args[]) {
		
		final ProtectedDemo ob = new ProtectedDemo();
		ob.display(); //calling a protected method inside another class within the same package
	}
}

Output:-
The value of a is : 5

The Private Access Modifier

Any member declared as ‘private‘ can only be accessed within the class where it is declared. Any class or interface cannot be declared as ‘private‘. The ‘private‘ access specifier is the most restricted access specifier.

class Demo {
	private int a=5;//declaring variable a as a private member of class Demo
}

/*Creating a subclass of the class Demo*/
class PrivateDemo extends Demo {
	void display() {
		System.out.println("The value of a is : "+a);//accessing variable a inside a subclass. This line will show an error message.
	}
}

/*Creating another class within the same package*/
public class TestPrivate {
	
	public static void main(String args[]) {
		PrivateDemo ob = new PrivateDemo();
		ob.display();
		
		Demo obj = new Demo();
		System.out.println("The value of a is : "+obj.a);//accessing variable a inside another class of the same package. This line will show an error message.
	}
}

In this Code, we see that a private member of the class Demo2 is being accessed inside a subclass and also a class within the same package so we get an error in line 8 and line 18.

Output:-
TestPrivate.java:8: error : a has private access in Demo2
                System.out.println("The value of a is : "+a);//accessing variable a inside a subclass. This line will show an error message.
TestPrivate.java:18: error : a has private access in Demo2
                System.out.println("The value of a is : "+obj.a);//accessing variable a inside another class of the same package. This line will show an error message.

The Default Access Modifier

The class or its members that do have any of the three access specifiers mentioned in its declaration falls under the default access modifier or the package access specifier. These members are accessible within any class present under the same package.

class Demo {
	int a=5,b=10;//no access specifier given to variable a and b. They fall under the default access specifier.
}

/*Creating a subclass of class Demo*/
class DefaultDemo extends Demo {
	void display() {
		System.out.println("The value of a is : "+a); //variable a is easily accessible inside a subclass
	}
}
  
/*Creating another class within the same package*/
public class TestDefault {
	
	public static void main(String args[]) {
		
		DefaultDemo ob = new DefaultDemo();
		ob.display();
		System.out.println("The value of b is : "+ob.b);//accessing variable b inside another class within same package.
	}
}

Output:-
The value of a is : 5
The value of b is : 10

An investment in knowledge always pays the best interest. Hope you like the tutorial. Do come back for more because learning paves way for a better understanding.

Do not forget to share and Subscribe.

Happy coding!! 🙂

Recommended -