Access modifiers are keywords used in programming languages, including Java, to control the visibility and accessibility of class members, such as variables and methods. They allow developers to specify the level of restriction placed on these members and determine how they can be accessed by other developers or code.
In Java, there are four types of access modifiers: public, private, protected, and default (also known as package-private). Each modifier serves a specific purpose in managing access to class members.
To better understand this, let's look at some examples.
As a person, there are some topics or incidents that you don't want to share or discuss with anyone.
You can mark them as private.
For example,
- The day you accidentally deleted some user's data from the production database 💻.
- Or your childhood crush - Jennifer Connelly ❤️.
Now there are some pieces of information about your life that you willingly share with everyone.
You can mark them as public.
For example,
- Your name and Snapchat ID.
- Or your selfie with your latest iPhone 📱.
Now, let's say that you are a member of the Flat Earth Society 🌍. You cannot discuss some topics with anyone else except other members of that group.
You can label them as default.
For example,
- You found a new mathematical formula ✏️ to prove that the Earth is flat.
- Or You visited the North Pole and found a huge wall on the edge ❄️.
You can only share these things with people who belong to that group.
Finally, let's talk about the protected access modifier. Let us say there is something that you can share with your family members, your children, and your grandchildren.
For example,
- You can talk about how access modifiers work 📚.
- Or you can just brag about how you have played through all the Dark Souls games ⚔️.
In programming, access modifiers work similarly. They allow you to control the visibility and accessibility of class members, just like you control what you share privately, publicly, within specific groups, or with specific individuals. By using access modifiers appropriately, you can ensure the right level of privacy, security, and encapsulation in your code.
Let's look at some code examples
File structure.
We have three different packages FlatEarthSociety, Family, and Office.
- In the FlatEarthSociety package, we have two classes, Vickie and Max.
- In the Family package, we have one class MaxJunior derived from Max.
- And In the Office package, we have one class, Thomas.
Max.class
public class Max {
public String name = "Max";
public String latestIphone = "iPhone 14 Pro Max";
private String childhoodCrush = "Jennifer Connelly";
boolean iBelieveInFlatEarth = true; // DEFAULT
protected int numberOfDarkSoulGamesPlayed = 8;
}
Thomas.class
public class Thomas {
public void display() {
Max max = new Max();
System.out.println(max.name);
System.out.println(max.latestIphone);
}
}
Thomas can access only "name" and "latestIphone" data members.
As Thomas belongs to a different package, he can only access the public members of Max.
Vickie.class
public class Vickie {
public void display() {
Max max = new Max();
System.out.println(max.name);
System.out.println(max.latestIphone);
System.out.println(max.iBelieveInFlatEarth);
System.out.println(max.numberOfDarkSoulGamesPlayed);
}
}
Vickie can access "name", "latestIphone", "iBelieveInFlatEarth" and "numberOfDarkSoulGamesPlayed" data members.
As Vickie belongs to the same package, he can access public, protected, and default data members. But cannot access private data members.
MaxJunior.class
public class MaxJunior extends Max {
public void display() {
MaxJunior grandpa = new MaxJunior();
System.out.println(grandpa.latestIphone);
System.out.println(grandpa.name);
System.out.println(grandpa.numberOfDarkSoulGamesPlayed);
}
}
Despite being present in a different package, MaxJunior can access public and protected data members of Max class.
This is because it inherits the Max class.
Last but not least, No one can access Max's childhoodCrush ❤️ except for Max because it's private!
Top comments (0)