Solved

OOPS terms

Posted on 2009-04-15
9
2,296 Views
Last Modified: 2012-05-06
hello experts,
i am learning C++.
please tell me what is difference between Data Abstraction, Data hiding and data Encapsulation.

Also, please, does C support Data Abstracion ?
0
Comment
Question by:sctt_tiger
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +2
9 Comments
 
LVL 40

Accepted Solution

by:
evilrix earned 25 total points
ID: 24147842
Some general OOP terms defined...

-          Inheritance

Means to derive behaving from a more generic type and implement a specialization of that type. For example, if we have a class called animal and we sub-class that to create a class called cat we have inherited all the behavior of an animal and then extend it to add behavior specific to a cat.

-          Abstraction

An abstraction is a generalization of something. For example we might be writing a game that has a boxer and a sumo wrestler, both of tese could derive from an abstract class called fighter. The class fighter is an abstraction of the basic ingredients required to represent a fighting character, which must then be sub-classed (see inheritance) to provide the more specific specialized behavior. The fighter class might not even implement any functionality, it might just provide an interface that all fighter types must implement. If your fighter class is well defined you can interact with any sub-class (boxer, wrestler, kung-foo master) via the abstract class interface.

-          Polymorphism

This is the ability for a type to be used within the context of its base class type yet the compiler will automatically perform the behavior of the sub-class type it refers to. For example, you make have a class of type boxer, which if currently pointed to by a pointer of the abstract fighter type. When you call the punch() method on fighter, which is an abstract method, the compiler will ensure that what actually happens is that the punch method for the boxer is called.

-          Encapsulation
This is information hiding, you might have a car and you know how to start the car, turn the wheels, accelerate and break. You know how to do this through a simple interface, you don't need to know how it all works, the detail of this is hidden from you. Because of this the maker of the car and change how their car works so that their next model has a completely different engine and other parts, but the interface is identical (steering wheel, breaks etc..) so you can still drive it. If you interacted with the detail and they changed it you'd have to re-learn how the new car worked!

Data-hiding is an artifact of abstraction and encapsulation. It's where we prevent objects from outside a class knowing anything about the class internals.

More info: -

http://en.wikipedia.org/wiki/Object_oriented
http://www.cplusplus.com/doc/tutorial/polymorphism.html

   
0
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 25 total points
ID: 24147989
data abstraction : a distinction is made between the (abstract) interface of a data type, and its (concrete) implementation. The user of the data type will interact with it using the provided interface, and the implementation will be hidden from the user.
Look at it as a black box (http://en.wikipedia.org/wiki/Black_box) : all you need to know is how to use it - how it works internally is not relevant.

data hiding : any part of the code should only have access to that data that it needs to use. Hiding all data that it doesn't need, makes sure that it can't inadvertently modify it either.

data encapsulation : making the class member data private, and only allowing manipulation of that data through well defined class methods.

You'll notice that these terms are very similar. That is because they deal with related issues. But they're not exactly the same, as I hope you can see from the above definitions.


>> Also, please, does C support Data Abstracion ?

Sure. Not in exactly the same way as an object oriented language of course, but data abstraction does exist. You can use pointers and structs to abstract data for example.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 24148011
Heh, seems I'm quite a bit slower in typing lol.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 24151501
For 'data abstraction' I found at Wikipedia (http://en.wikipedia.org/wiki/Data_abstraction)

(1)
the mechanism and practice of abstraction reduce and factor out details so that one can focus on a few concepts at a time.

and

(2)
abstraction - a concept or idea not associated with any specific instance

and

(3)
Data abstraction allows handling data bits in meaningful ways. For example, it is the basic motivation behind datatype.

The (3) is similar to that what Infinity said. And (1) may express somewhat like  the 'generalization' issue evilrix mentioned.

For 'data hiding' I found:

Information hiding in computer science is the principle of hiding of design decisions in a computer program that are most likely to change, thus protecting other parts of the program from change if the design decision is changed. The protection involves providing a stable interface which shields the remainder of the program from the implementation (the details that are most likely to change).

Here evilrix seems to have the same focus on the fact that the main benefit of data hiding is to being able to change or improve an implementation without having to change the usage.


And finally for 'encapsulation'

encapsulation is the hiding of the internal mechanisms and data structures of a software component behind a defined interface, in such a way that users of the component (other pieces of software) only need to know what the component does, and cannot make themselves dependent on the details of how it does it. The purpose is to achieve potential for change: the internal mechanisms of the component can be improved without impact on other components, or the component can be replaced with a different one that supports the same public interface.

Encapsulation also protects the integrity of the component, by preventing users from setting the internal data of the component into an invalid or inconsistent state.

The second part is that what Infinity has told about data hiding. The 'private members' surely is a C++ specific way of encapsulation. Both Infinity and evilrix also mentioned the near relationship of 'data hiding' and 'encapsulation'.


 
 
0
 

Expert Comment

by:dmxflames
ID: 25413797
Much has been already nicely described...

Simply:

  1. Data abstraction involves resolving "real-world" information to a programmable form involving data structures.
  2. Data hiding is restricting data for use by code (as descibed also by Infinity 08)
  3. Data encapsulation concerns defining access scopes to data in objects so as to provide a safe-way in dealing with data. This is most useful for properties of classes that need controlled external access. In particular, public 'setters' and 'getter' are used to expose and access field members which have been scoped private.


0
 
LVL 40

Expert Comment

by:evilrix
ID: 25419004
>> Data hiding is restricting data for use by code (as descibed also by Infinity 08)
I think you'll find he wasn't the only one who mentioned this.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question