DEV Community

loading...
Cover image for What is Encapsulation?

What is Encapsulation?

Keshav Kumar
I am college student and java lover. Yes i really love this language.
・1 min read

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,
Alt Text
and now we can get the data through get method,
Alt Text
}

Discussion (1)

Collapse
lifeofdekisugi profile image
Shahir Islam

wow, Thanks man