Hey guys
Good day!
Today I learned one of the four pillars of OOP concepts that's wrapped inside the blog.
As I said, wrapped—guess what? It's nothing but encapsulation.
In encapsulation, as of now, I learned access modifiers and getter-setter methods. We will see them in detail in the blog.
Access Modifiers:
Definition : Access level modifiers determine whether other classes can use a particular field or invoke a particular method.
There are two levels of access control:
- At the top level—public, or package-private (no explicit modifier).
- At the member level—public, private, protected, or package-private (no explicit modifier).
Types of Access Modifiers:
- Public
- Default or package- private
- Private
- Protected
Public :
As the name gives it can be accessed by all
All means same class , same package child class, different package child class, same package non-child class ,different package non-child class.
Default or Package-Private :
As the name suggests it can be accessed only within the same package
Private :
As the name suggests , it is private to the parent classs, it cannot be accessed by no one outside the class.
Protected :
It is bit tricky when I practiced it. It can accessed by any class within the same package and by sub classes in the different packages.
Differentiation summary :
Getter and Setter methods :
Why we need that method :
Class 1:
public class GRT
{
private int price = 14200;
//getter method
public int getPrice(){;
return this.price;
}
public void setPrice(int price){
if(price >= this.price)
this.price = price;
else
System.out.println("Amount is less then fixed value");
}
}
Class 2:
public class Customer{
public static void main (String [] args){
GRT grt = new GRT();
// int itemPrice = grt.getPrice();
// System.out.println(itemPrice);
grt.setPrice(14500);
System.out.println(grt.getPrice());
grt.setPrice(14000);
}
}
Story for the above code :
There is a jewellery shop called GRT. Every day, the gold rate goes up and down. A customer visits the jewellery shop. Note that if the jewellery shop keeps the gold price public, the customer can access and change the price to whatever he or she wants. That is why we keep the price private.
When the customer walks into the shop and asks for the current gold price, that is called the getter method. If the customer wants to change the price, there is only one way to do it: through the setter method.
Inside the setter method, we can apply any conditions, rules, and regulations because the shop belongs to us.
How can we connect it with Encapsulation:
The gold price is kept private, so it cannot be accessed directly from outside the class.
The getter method is used to read or view the gold price.
The setter method is used to update the gold price while enforcing business rules and validations.
This is how encapsulation protects data and provides controlled access to it.
If you any doubts or clarifications feel free ask in the comment section because I am budding developer . Goodfeedbacks are always taken.
Let's see in the another Interesting Blog...

Top comments (0)