Access Modifiers
“What does a modifier do?”,” Do we really need them?”,” Can’t we just skip this part?”. If you’re pondering these questions, then this blog article is definitely for you.
First of all, let’s give a brief introduction to modifiers. Modifiers are actually keywords used in programming languages to alter the behavior of variables, methods, or classes. Yes, it is like an Asian parent who is always on the verge of disciplining their children.
There are two types of modifiers: Access modifiers and Non-access modifiers.
In this article, we’ll specifically explore access modifiers available in Java because they play a crucial role in defining the security of your code. As the prefix “Access” says, access modifiers limit the accessibility or the scope of the code. It adds extra layers of security to the program and restricts unauthorized access. Access modifiers can be applied to classes, attributes, methods, and constructors. We can change the access level using some special keywords.
Let’s consider the scenario for classes.
We can use either `public` or default (no modifier) for classes.
- Public classes are accessible by any class, it doesn’t matter if it is a subclass or a class in another package. The `public` keyword will be used as a modifier.
Syntax:
public class Student
{
//.......
}2. The default class is accessible by the classes which should be in the same package. It can not be accessed from outside the package. Even a subclass in a different package, can not have the accessibility for the default class. If a class is left without a modifier, then it is a default class.
Get Ragapiriya Rameshkumar’s stories in your inbox
Join Medium for free to get updates from this writer.
Syntax:
class Student
{
//.......
}Let’s consider the scenario for methods and attributes.
There are four types of such access modifiers.
- `public` — 🥷
- `protected` — 🥷🥷
- default -🥷🥷🥷
- `private` -🥷🥷🥷🥷
Down the column, protection over the methods and attributes increases. And even when you move diagonally along the table, you will find a lot of “NO”s. It means the higher the restriction level, the lower the accessibility. “NO” stands for “No access”.
- `public`: Visible to any classes. No restrictions at all.
- `protected`: Can be accessed by all classes in the same package and by a different class in a different package as long as that class is a subclass of whatever the class contains a protected member.
- default: Can only be viewed by anything in the same package, not by anything in another package.
- `private`: Access level is restricted within the same class.
The following example demonstrates the different levels of access control you can apply to fields and methods within a class.
public class Student
{
public String studentName;
protected int age;
String studentID;
private double gpa;
//public method: accessible from anywhere
public void displayInformation()
{
System.out.println("Name: " + studentName);
System.out.println("Age: " + age);
System.out.println("Student ID: " + studentID);
System.out.println("GPA: " + gpa);
}
//protected method: accessible within the same package and subclasses
protected void updateAge(int newAge)
{
this.age = newAge;
}
//default method: accessible only within the same package
void updateStudentID(String newID)
{
this.studentID = newID;
}
//private method: accessible only within this class
private void setGPA(double newGpa)
{
this.gpa = newGpa;
}
}Understanding and effectively using access modifications is crucial for writing secure and maintainable code. By controlling the visibility of classes, methods, and attributes, you can encapsulate the internal workings of your code and prevent unintended interactions. This practice not only helps protect the integrity of your data but also enhances the code readability and reduces the potential bugs. Mastering access modifiers will empower you to design robust object-oriented systems and ensure that your code remains both functional and secure.
Good luck.








