GeekInterview.com
Series: Subject: Topic:
Question: 203 of 576

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

Asked by: gurvinder372 | Member Since Aug-2006 | Asked on: Mar 10th, 2007

View all questions by gurvinder372   View all answers by gurvinder372

Showing Answers 1 - 6 of 6 Answers
Anuj Gupta

Answered On : Mar 17th, 2007

Abstractionmeans 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.

  
Login to rate this answer.

Could you please post some examples also for each of them preferably java code snippet. That will be really helpful

  
Login to rate this answer.
venkat J

Answered On : 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

Yes  1 User has rated as useful.
  
Login to rate this answer.
sampra

Answered On : Mar 11th, 2008

View all answers by sampra

all are logically same

  
Login to rate this answer.
lhariPrasad

Answered On : Mar 18th, 2008

View all answers by lhariPrasad

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.

  
Login to rate this answer.

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'.

  
Login to rate this answer.

Give your answer:

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

Related Open Questions

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Interview Question

 Ask Interview Question?

 

Latest Questions

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.