What is Encapsulation in Java?

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 …


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


Print Share Comment Cite Upload Translate Updates
APA

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/

MLA
" » What is Encapsulation in Java?." Keshav Kumar | Sciencx - Thursday June 3, 2021, https://www.scien.cx/2021/06/03/what-is-encapsulation-in-java/
HARVARD
Keshav Kumar | Sciencx Thursday June 3, 2021 » What is Encapsulation in Java?., viewed ,<https://www.scien.cx/2021/06/03/what-is-encapsulation-in-java/>
VANCOUVER
Keshav Kumar | Sciencx - » What is Encapsulation in Java?. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/06/03/what-is-encapsulation-in-java/
CHICAGO
" » What is Encapsulation in Java?." Keshav Kumar | Sciencx - Accessed . https://www.scien.cx/2021/06/03/what-is-encapsulation-in-java/
IEEE
" » What is Encapsulation in Java?." Keshav Kumar | Sciencx [Online]. Available: https://www.scien.cx/2021/06/03/what-is-encapsulation-in-java/. [Accessed: ]
rf:citation
» What is Encapsulation in Java? | Keshav Kumar | Sciencx | 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.

You must be logged in to translate posts. Please log in or register.