DEV Community

Cover image for What is Encapsulation?
Keshav Kumar
Keshav Kumar

Posted on

What is Encapsulation?

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
}

Top comments (2)

Collapse
 
linehammer profile image
linehammer

Encapsulation is one of the fundamentals of OOP (object-oriented programming) and it is used as part of abstraction. Wrapping up data member and method together into a single unit (i.e. Class) is called Encapsulation. This means that a class publishes only what is needed for others to use it, and no more. This is called information hiding and it means classes can totally change their internals without having an effect on any of their users. It is a broader concept of object-oriented programming that consists in minimizing the interdependence between classes and it is typically implemented through information hiding.

Eg: we can consider a capsule. Encapsulation means hiding the internal details of an object, i.e. how an object does something. Here capsule is a single Unit contain many things. But we cant see what is there in side capsule.

net-informations.com/faq/oops/enca...

Collapse
 
lifeofdekisugi profile image
Shahir Islam

wow, Thanks man