Encapsulation in java

It is one of the most basic pillars of object oriented programming language.

Wrapping up of data and methods in a single unit is called Encapsulation.
It is like a protective shield that prevents the data from being used by the code outside the shield.

Let's undetstand access modifiers in java first.

Acess modifiers define the scope of the class and its elements.

There are mainly 4 types of access modifiers in java.



So, you can not use private variables outside the class. so to solve this we use getter and setter methods. 

Getter and setter methods are ordinary methods but they encapsulate the fields of the class making them accessible only through public methods and keep the values themselves private.





Comments

Related Posts Plugin for WordPress, Blogger...