开发者

Difference in Information hiding and data abstraction?

开发者 https://www.devze.com 2023-03-31 18:54 出处:网络
Is there any difference in Data Abstraction and Information hiding? After going through all the answers in this link I am more confused.

Is there any difference in Data Abstraction and Information hiding? After going through all the answers in this link I am more confused. Abstraction VS Information Hiding VS Encapsulation Couldn't find any difference. Is it just that we can call one (info hiding) as a goal & the other (abstraction) as a process? But this is 开发者_C百科no satisfactory difference for me. Further, I got that encapsulation is the technique to implement the process of abstraction Am I right here? Please explain the exact difference.


Information hiding is when the designer specifically decides to limit access to details of an implementation. It's a principle that's older than object-oriented design, but is often used.

A simple example is defining constants in C, e.g., #define NAME_SIZE 15 The code (clients) of the constant don't need to know its value, and won't be troubled if you (the designer) decide to change its value later. They shouldn't make assumptions about the fact that it's really 15, because you might decide to change it.

Abstraction is when you're dealing with an aggregate, e.g., a Car is an abstraction of details such as a Chassis, Motor, Wheels, etc. Abstractions allow us to think of complex things in a simpler way.

Encapsulation is how we decide the level of detail of the elements comprising our abstractions. Good encapsulation applies information hiding, to enforce limits of details. For example, my Car is comprised in reality of all its parts, yet it only provides to me (the driver) an interface that's appropriate for my needs and not more. I can control the doors, locks, windows, lights, horn, sunroof, the direction of the movement, accelerate, decelerate, etc. Even though I might be curious to manipulate the details of the "how" of all these things, encapsulation prevents me from seeing more.

If my car's implementation changes (I change from a combustion engine to an electric or hybrid), because I as the driver know only the limited interface, I don't need to change how I drive the car. Abstraction allows me to just know I'm driving a car, instead of hundreds of pieces of metal, rubber, etc.

An example of where information hiding was not part of a car might be a choke valve. My parents told me how those used to work in the cars they drove... it was a combustion-engine detail, which would not be useful in an electric car.


Data hiding is the process by which access modifiers are used to hide the visibility of java methods and variables. They access modifiers are: public, private and protected.

Abstraction is the process by which we define a specific behavior by beans of abstract classes and methods which form the skeleton for any class that would be extending this class.


"Information hiding" is an important PART of "Data abstraction", but not the whole concept.

And remember: you can (and should) have "information hiding" in procedural code (like "don't use globals", etc in FORTRAN or BASIC) - but you won't necessary have an "abstract data type".

Information hiding and Abstract Data Types are closely related, but they are different concepts.


A class normally hides its implementation details from its clients. This is called information hiding. by creating interfaces we summon information hiding concept...

example of information hiding is below... we have a interface in our header file...

class Coder
{
   public: 
       Coder();
       void prints();

private:
       int x;

};

and implementation of functions in another file "Coder.cpp" is...

Coder::Coder
{
 x=10;//any int value you can take;
}

void Coder::prints()    
{
   cout<<x;
 }

rather tahn doing above in two files (one header+one cpp file) we could have done it at a single place. we could have given defination of constructor and print function in header file itself...

  class Coder
    {
       public: 
           Coder()
            {
              x=10;//any int value you can take;

             }
           void prints()
    {
           cout<<x;
         }

    private:
           int x;

    };

if we have done this we were not able to implement information hiding... and our client will know how we have implemented our functions!

for data absraction you can consider... example of stacks...

A client of a stack class need not be concerned with the stack's implementation. The client knows only that when data items are placed in the stack, they will be recalled in last-in, first-out order. The client cares about what functionality a stack offers, not about how that functionality is implemented. This concept is referred to as data abstraction.


Abstraction is the representation of something with less details (as in an abstract painting). In OO, an abstract type can be manipulated without committing to its internal representation. For example, Telephone Number as an abstraction of a telephone number can be operated on without the client knowing that it consists of country code, area code, and the actual number. Abstraction is most useful in the analysis and design phase because it allows you to talk in terms of the abstract data type (eg. Telephone Number) without having to worry how it will be implemented.

A more familiar type, string is an abstraction of text: you manipulate string without knowing how it is implemented. The string abstraction allows its internals to be changed without affecting its usage in an application design.

Information hiding and encapsulation are two ways in which an abstract data type might be implemented. An abstract data type might not even have to hide its internal state or its encapsulation; for example, Number as an abstraction may be implemented as an int.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号