• Post Reply Bookmark Topic Watch Topic
  • New Topic

Abstraction vs Encapsulation  RSS feed

 
raj malhotra
Ranch Hand
Posts: 288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi i have some confusion related to these terms...please have a look if i am correct or not:-

Abstraction means- hiding implementation.
encapsulation means-hiding data.

Is above definitions are correct.If yes How these two terms are related? I mean can these things exist without each other?

Thanks in advance to go through this post..
 
Shoumin Li
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
encapsulation means-hiding data.
This is right in part.
Abstraction means- hiding implementation.
But has question.
The forum has such thread about this.
http://www.coderanch.com/t/400260/java/java/Data-abstraction-vs-Data-Encapsulation
 
Francesco Bianchi
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by raj malhotra:

encapsulation means-hiding data.

This is HOW you achieve the real aim: PROTECTING data.

Originally posted by raj malhotra:

Abstraction means- hiding implementation.


Not at all. What about if you make an abstract class with only public members? Abstraction, as it goes for classes, is more a way to group common code in a unique ancestor class when this come up in a class which has no sense to be instantiated on his own. So you mark it abstract and let the work to end the implementation to the extending classes.
For what concerns methods it is a LITTLE different. An abstract method says: "I know for sure that I'll need a method with this signature which does something with this input and gives me that output but I can't (or simply explicitly don't want to) find at all a default implementation". Moreover this way you make clearer to the extending classes developers where they have to write their code. I.e. an abstract class which handle all the data access mechanisms and an implementation which has methods like



I would say that:
Abstraction means- making implementation more general.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I prefer to look at abstraction as ignoring unimportant details so we can focus on the important bigger picture. If I work with a variable of type List I can focus on the List operations and ignore implementation details like whether it's a LinkedList, an ArrayList or SomeNewList. The List abstraction doesn't hide anything. It just tells me the minimal information I need to work with it.

Of course we can make up a scenario where I care about more details. Maybe I'm going to build a list once and do a million gets by index. Maybe I'm going to do millions of adds on the end and removes from the front. Then I may need to drill a level down in abstraction to learn which kind of List performs better. I still probably don't want to see the source code of an ArrayList or LinkedList. They are lower abstractions, but there's still a lot I can ignore.

Learning to "not care" about what's behind the curtain is hard some times.
 
Bert Bates
author
Sheriff
Posts: 8954
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i'm thginking this is at least an intermediate level topic - those intermediate level moderators might decide it's an OO design sort of topic...
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's a great definition of abstraction in the introduction to Algorithms and Data Structures by Wirth (big thanks to Ulf Dittmer for posting a link to that recently).

The information that is available to the computer consists of a selected set of
data about the actual problem, namely that set that is considered relevant to the problem at hand, that set
from which it is believed that the desired results can be derived. The data represent an abstraction of reality
in the sense that certain properties and characteristics of the real objects are ignored because they are
peripheral and irrelevant to the particular problem. An abstraction is thereby also a simplification of facts.

We may regard a personnel file of an employer as an example. Every employee is represented (abstracted)
on this file by a set of data relevant either to the employer or to his accounting procedures. This set may
include some identification of the employee, for example, his or her name and salary. But it will most
probably not include irrelevant data such as the hair color, weight, and height.

In solving a problem with or without a computer it is necessary to choose an abstraction of reality, i.e., to
define a set of data that is to represent the real situation. This choice must be guided by the problem to be
solved. Then follows a choice of representation of this information. This choice is guided by the tool that is
to solve the problem, i.e., by the facilities offered by the computer.

[ July 13, 2007: Message edited by: Garrett Rowe ]
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One good example of abstraction in Java is its support for multi-threading. I'm almost certain that under the hood, there is a complicated process of allocating and deallocating this and that, timers and schedulers, gears and motors whirring and buzzing, all in all some pretty heavy-duty computing. I say almost certain because I've never looked, and don't want to if I don't have to. Lucky for me, the Thread class and Runnable interface abstract all that business away from me. All I have to know is how to implement the Runnable interface, and how to pass that to a Thread and call start().
[ July 13, 2007: Message edited by: Garrett Rowe ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!