What is the difference between abstraction, encapsulation and data hiding?

Questions by gurvinder372   answers by gurvinder372

Showing Answers 1 - 6 of 6 Answers

Anuj Gupta

  • Mar 17th, 2007
 

Abstraction means hiding the code by defining member functions.


Data hiding means we are providing security to data within the class.


Encapsulation is the combination of abstraction and data hidingm, means we are wrapping data and code associated with that data.

  Was this answer useful?  Yes

venkat J

  • Apr 18th, 2007
 

Abstraction and encapsulation are two quite separate concepts in Java. Abstraction is a technique that is used to represent common functionality amongst a set of classes. An analogy is to look at a set of vehicles: Car, Truck and Bus have some common features that all road vehicles share. From a Java perspective, the mechanics of turning four wheels, an engine, could be handled in abstract form through a common super class. Abstraction allows subclasses to share common code without duplication. Encapsulation is hiding the data within the classes by providing public methods to manipulate the data

we declare the variables as private and provide public methods to manipulate from outside

lhariPrasad

  • Mar 18th, 2008
 

Venkat,
The thing which you mentioned can also be done by Inheritance, then what is the need of Abstraction over here in Java. Is it that much important ?
I feel Abstraction is something where a specific template like structure is created for the implementators, aiming to provide a common interface.

  Was this answer useful?  Yes

1. Encapsulation means Binding the Data with its relative functions. That means only those functions can access the data, and no other function can access that data.

This can be achieved by classifying the relative data members and relative functions as a unit called CLASS
. Encapsulation is achieved through a class in java. So every class denotes a separate encapsulated unit. One class functions can not directly access other class data.

Thats how security is provided for Data and complexity is reduced when reengineering the project because of the separation of the code into simple and individual classes.

2. Abstraction means Hiding the unnecessary data and showing/considering only the neccessary data.

Abstraction should be considered at the time of designing a class.

This can be understood with the follwing example.
Suppose if we want to design a Clock class. In this Clock class, we can define Clock properties like : length, width, height, color, shape, style,...... .
 But we need not define all those properties. If our client/user needs only length, width and color of the Clock, then we should consider and define only those as the properties of the class Clock and can neglect/hide other unneccessary properties. This is Abstraction.


When designing a class, first Abstraction is considered. So that what are the properties to be taken are decided.
Then Encapsulation is applied, to separate these properties and this class' functions from other class'.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

Answer Question

Click here to Login / Register your free account


 
Send   Reset

 

Related Answered Questions

 

Related Open Questions