Encapsulation explanation | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Encapsulation explanation

I couldn't understand public, private and all of such types of terms

13th Apr 2017, 3:25 PM
Legend Shees
Legend Shees - avatar
1 Answer
+ 15
First of all, don't be too ambitious ,as you said these 3 concepts are related (especially the first two) and could be used for one another in many contexts. Using them correctly is much more important than having a complete final definition. "data hiding" is all about putting a wall between the client and (part of) the implementation. Some objects of a module can be internal to the module and invisible to its users. As such, this is a way, a method to avoid dependency. if I cannot know how one thing is implemented, its implementation can change. "data abstraction" is regrouping different kind of data under the same abstraction. It is close to the idea of a protocol. You don't know how the object is implemented, but you know it respect a well-known protocol, i.e a set of method that works over different type of data. In python, file-like object are a good example. In Java, one uses interfaces. It is good because you have less to learn, and also because you can check some properties at the abstraction level, i.e for all kind of data regrouped under this abstraction. "encapsulation" is about putting a shell around objects that simplify their usage. it is linked to the idea that objects in a code base can be regrouped in layers increasingly low-level. One object in a layer calls only those of layers beneath him. For example, if you want to draw a line on the screen, the line obkect may only encapsulate an openGL context, the pixel drawer, and other stuff. These lower-level objects are encapsulated by the line object. Note the encapsulation can be applied to the same object when it is part of different layers at the same time, not good but sometimes unavoidable. For example, the file-like object in python have high-level/encapsulating method (open, close, read) and low-levels ones (seek). That's it. Obviously, the definition of each could be broader but these make the three concepts a bit more different. source: Stackoverflow
13th Apr 2017, 3:29 PM
Dev
Dev - avatar