The object oriented concepts : encapsulation, data abstraction and data hiding are 3 different concepts, but very much related to each other. So i am having difficulty in understanding the concepts fully by reading the information from internet. The information available at one place contradicts with information at another place in the internet. Could someone guide me to a tutorial which clearly explains the 3 concepts and brings out the difference between the three?

有帮助吗?

解决方案

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.

其他提示

The wrapping up of data and functions into a single unit (called class) is known as encapsulation. Data encapsulation is the most striking feature of a class. The data is not accessible to the outside world, and only those functions, which are wrapped in the class, can access it. These functions provide the interface between the object’s data and the program. This insulation of the data from direct access by the program is called data hiding or information hiding. Abstraction refers to the act of representing essential features without including the background details or explanations. Classes use the concept of abstraction and are defined as a list of abstract attributes such as size, weight and cost, and functions to operate on these attributes. They encapsulate all the essential properties of the objects that are to be created. The attributes are sometimes called data members because they hold information. The functions that operate on these data are sometimes called methods or member functions. Since the classes use the concept of data abstraction, they are known as Abstract Data Types (ADT

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top