This content originally appeared on DEV Community and was authored by Keshav Kumar
Encapsulation is one of the fundamental units of object-oriented programming. It simply means that putting all the data at one place, Binding And Wrapping of data and methods together is called Encapsulation.
Through encapsulation we can achieve data hiding, if you are familiar with any object oriented programming language then you must be familiar with getters and setters method. When we try hide the data then we use "private" access modifier to stop the any kind of modification of attributes. Now the question arises if we have stopped any modification then how will we change or get the value of elements, so for that only getters and setters methods are used.
Getter method is used to return the value of that element and setter method is used to put the value in that element.
Now lets understand it with an example:
private int price;
Here the price will not be accessible outside of the class, but we can set the data through set method,
public void setPrice(int price){
this.price=price;
}
and now we can get the data through get method,
public int getPrice(){
return price;
}
This content originally appeared on DEV Community and was authored by Keshav Kumar
Keshav Kumar | Sciencx (2021-06-03T09:02:51+00:00) What is Encapsulation in Java?. Retrieved from https://www.scien.cx/2021/06/03/what-is-encapsulation-in-java/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.