Java Access Modifiers: default, private, protected, public
Java Access Modifiers: default, private, protected, public
Introduction
If you’re learning Java, access modifiers are an essential concept to get your head around. Access modifiers allow you to define which sections of code can be seen or used by other classes, and understanding them is key to writing efficient programs. In this blog post, we’ll explain each of the four major Java access modifiers in-depth – public, private, protected and default – what they do differently, when it’s appropriate to use each one and how they interact with inheritance. Get ready to boost your confidence in being able to use Java appropriately!
What is an Access Modifier in Java?
Access Modifiers in Java generally control the permissions of a block of code. It assists the accessibility and visibility of the program by defining which part of the program would be visible to the users as well as other members.
It provides security and authentication to the program. If the developer wants to allow the code to be publicly available it might need some undesired changes. To avoid this situation, developers control the access of the program by using Access Modifiers. They segregate the code to be public, private, or protected according to their needs.
Real Life example of an Access Modifier in Java?
As a real-time example, we can choose Facebook where users can control their posts. There are three types of Access Modifier such as, "Public Access Specifier", "Protected Access Specifier" and "Private Access Specifier"
- If anyone wants to make the status visible to the public they can choose "Public Access Specifiers".
- If anyone wants to make their status visible to only their friends it is called a "Protected Access Specifier".
- Lastly, if someone wants to make the status visible for only themselves then they can use the "Private Access Specifier".
Types of Access Modifiers in Java
There are four types of Access Modifier in Java which are
- Default Access Modifier
- Private Access Modifier
- Protected Access Modifier
- Public Access Modifier
Default Access Modifiers in Java
If any data or the developers of that program do not specify any access modifier then that means it is in the default state. In the default stage, all the methods and classes including the data members are package-level. Default Access Modifier in Java means the methods and classes are accessible only in one particular or same package, which is a collection of related classes.
Example
// Java program to show default modifier
package p1; //Package named P1 declaration
// Class default_DNT is having Default(Package-level) access modifier
class default_DNT
{
void show()
{
System.out.println("Use of Default Access Modifier");
}
}
package p2; //Package named P2 declaration
import p1.*; //Importing P1 to P2 for accessing the class member functions
class Main1
{
public static void main(String args[])
{
//Trying to access class default_DNT from package p1
default_DNT obj = new default_DNT();
obj.show();
}
}
Output
Compile time error
Private Access Modifiers in Java
Private Access Modifier in Java is a private keyword that is mainly used for a private class, constructor, or method. The methods of private modifiers can not be accessed by other users rather than their native class. The data members of private modifiers are only available in their defined class.
Example
class Access_Modifier
{
// private variable
private String str;
}
public class Main
{
public static void main(String[] main)
{
// create an object of Access_Modifier class
Access_Modifier a = new Access_Modifier();
// Try to access private variable from another class
a.str = "This is private";
}
}
Output
//AccessModifiers.java:24: error: a has private access in AccessModifiers
System.out.println("value of a (direct access)= " +obj.a);
Protected Access Modifiers in Java
The methods and the members of Protected Access Modifiers in Java are only accessible and visible to their classes and their inherited classes. Protected Access Modifiers use the keyword "protected" to do the declaration. This concept follows the Inheritance methods of object-oriented programming.
Example
// Class Parent is the Parent class
class Parent
{
protected void display() //protected method
{
System.out.println("Testing Protected Access Modifier");
}
}
//Class child inherits the Parent class
class Child extends Parent
{
public static void main(String args[])
{ //obj created to access the protected method display()
Child obj = new Child();
obj.display();
}
}
Output
Testing Protected Access Modifier
Public Access Modifiers in Java
Public Access Modifier in Java is used for making a method or data member accessible for everyone and every platform. This access modifier uses the "public" keyword to make the data public in Java.
Example
class AccessModifiers
{
public int a;
public int b;
public void setVal()
{
a=7;
b=3;
}
public void getVal()
{
System.out.println("a= " +a);
System.out.println("b= " +b);
}
}
class mainDemo
{
public static void main(String args[])
{
AccessModifiers obj = new AccessModifiers();
//Can directly access public members
System.out.println("Value of a (direct access)= " +obj.a);
System.out.println("Value of b (direct access)= " +obj.b);
obj.setVal();
obj.getVal();
}
}
Output
Value of a (direct access)= 0
Value of b (direct access)= 0
a= 7
b= 3
Summary
This article gives a clear idea about the Access Modifiers in Java and their types. In this article, every type of Access Modifier has been vastly evaluated with examples. It's clear to see the strength and value of Java access modifiers. They can be seen as the 'gatekeepers' that help protect and secure our code, control the data privacy within a system, and aid in keeping object-oriented programming consistent. We hope this guide gave you an in-depth look into how each of these access modifiers works and how you can use them to protect your code from malicious actors or users. It's up to you now to practice using the various access modifiers and protect your coding projects!
Take our free skill tests to evaluate your skill!

In less than 5 minutes, with our skill test, you can identify your knowledge gaps and strengths.