• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

difference between abstraction and encapsulation

 
Hamraj Kulshreshtha
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please tell me the exact difference between abstraction and encapsulation
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, welcome to the ranch!

We love to help but we don't "do homework", so a question like this is not likely to get direct answers.

Show us your thoughts first. Are you comfortable with some definition for each? How did they seem similar or different to you?
 
Hamraj Kulshreshtha
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok

What i know is like this:

Abstraction means puttting all the variables and methods in a class which are necessary i.e, collection of data.

Encapsulation simply means the data hiding by means of modifiers such as public,private,protected.

Is my understanding is right?
If not then please help me regarding this.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you might have them backwards.

My favorite definition of abstraction is about ignoring the things that don't matter so you can focus on the things that do matter. How would that relate that to an abstract class or an interface in Java?
 
Hamraj Kulshreshtha
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you please explain me with some real example w.r.t coding,because your explanation is not so clear.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The List interface is an abstraction - it only defines the general behavior of a list, and leaves it open how it gets implemented.

Encapsulation actually means putting data and operations on that data together (for example in a class). Today, it often gets confused with information hiding, which is unfortunate.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To tie List back the the definition I gave, you can (usually) ignore how a List is implemented and focus on what it promises to do for you.

There are levels of abstraction. That method is at a high level because it doesn't care at all what kind of List is passed in. But some List implementations are faster at inserts and deletes while others are faster at get by index. So you might decide that level of detail is important to you, but you can still ignore the actual code inside.

This goes contrary to the geek tendency to want to understand everything. From Knight's Principles: One of the great leaps in OO is to be able to answer the question "How does this work?" with "I don�t care".
[ October 08, 2007: Message edited by: Stan James ]
 
Saurabh Gupta
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi James,

You have defined Abstraction and Encapculation very clearly.
Thanks.
As stated by you "Today, it often gets confused with information hiding, which is unfortunate."

This means Data Hiding is completely different concept.
If so, could you please add more about data hiding.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That was Ilja's quote I think, but I'll jump in anyway. The term "information hiding" dates back to the 1970s and deals with abstraction in its own way. It says don't expose the details of how something works and the data it uses internally so that others won't become bound to those details. That way you can change the internal workings without bothering any of the clients, or even replace a whole component without breaking the clients.
 
amitkumar deshpande
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi James,
We are aware of definations of abstraction and encapsulation..
In java encapsulation is achieved by making use of access modifiers
( keeping variables as private and providing public getters and setters ).

On same lines, can you please explain how to achieve abstraction by
a simple java code ?
Amit
 
Mustafa Garhi
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So if someone asks me to show an example of bad abstraction using a java class i would never be able to do it. Because when i write a java method with some logic in it my caller will always only use the name of the method to call it without having to know the details so we are achieving abstraction by default everytime.

If at all i have understood the discussion before in the thread - myListOperation(List myList) is more abstract compared to myListOperation(List<String> myList). If so i think i am ruining my code in the sense that i am stopping the user from sending bad data at run time if i use myListOperation(List myList) using exception handling vs stopping the user from sending bad data at compile time using myListOperation(List<String> myList). I think compile time is better and i wouldn't prefer abstraction in that case.

Also if i use interface to hide the implementation, am i actually hiding anything - finally a user can always open the jar of my API and get to the class implementation.

If you see length() method of String class the only line of code in it is
return count;
and i thought this is abstraction because i don't know even after seeing the method how is length calculated before i have some more work to do.

Also if encapsulation is putting your methods and variables together in a class, whats the deal. Then everything is encapsulated in java.
 
Sudha Govind
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Encapsulatin:Wrapping up of the attributes and methods into a single unit i.e the class is called the encapsulation.

Abstraction means datahiding.i.e we are not allowing some information to be accessed at this time.
 
sheena pinky
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Encapsulation:Wrapping up of data
Polymorphism:Acting different forms
 
sheena pinky
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abstraction:Taking relevant things & leaving irrelevant things
 
Campbell Ritchie
Sheriff
Pie
Posts: 49847
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch, both of you.

No, abstraction and data hiding are different things.
 
Naresh Shanmugam
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you please let us know the difference with small java code snippet...
 
Campbell Ritchie
Sheriff
Pie
Posts: 49847
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the late Stan James wrote: . . . From Knight's Principles . . .
That says
Premature optimization leaves everyone unsatisfied


I would suggest you read the whole of this thread, then tells us what you don't understand.
 
Naresh Shanmugam
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My understanding is,

"Encapsulation is accomplished in java by using Class. - Keeping data and methods that accesses that data into a singe unit"
"Abstraction is accomplished in java by using Interface. - Just giving the abstract information about what it can do without specifying the back ground details"
"Information/Data hiding is accomplished in java by using Modifiers - By keeping the instance variables private or protected."

Please correct me if i am wrong
 
Manjusha Muraleedas
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abstraction means generalization. its more related to concept /behaviour in an application. Abstraction can be achieved by interface .

Considering the MyMethod example, MyMethod is declared to accept List interface, means anything which extends List interface can be passes to MyMethod. That is an example of Absraction.

Encapsulation means hiding the information from direct access of the user or hiding the internal implemetation details of data and method from user.
For eg: We have a class named Laundry. Laundry has a method called. The user of this method gives the number of cloths to be washed and gets the total fare in return ( + washed cloths ofcourse ) The class hides the information of how the cloths gets washed and what are all things are being used for washing.

Data hididng is more related to (or a part of ) encapsulation
 
Mustafa Garhi
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone write a code snippet that is less abstract?

Something where we are exposing implementation details to the code user?

Is myMethod(List<String> list) less abstract than myMethod(List list) ?

Thanks in advance
 
Campbell Ritchie
Sheriff
Pie
Posts: 49847
70
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, your List examples are not more or less abstract. This shows encapsulation . . . and this doesn't:
 
Mustafa Garhi
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Campbell. That was nice.

This means one way of achieving encapsulation is access specifiers in java(with obviously the right brain for the right logic). Am i right?

Also, it is said that interfaces are a good way of achieving abstraction. Can you explain how?

And one more thing, if you or someone else could put an example of a badly abstracted class here.

Thanks again.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49847
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One of the ways to maintain encapsulation is to mark all fields (except those used as constants) private. But that is only part of it.

Look at my plant examples and the older version here. The interface, as you see from the postings, has three methods, but doesn't tell us how it is implemented. The concrete example there has "litres" as a field, but other implementations (eg ElectricEngine) might have kilowatts or horsepower as their fields. The useFuel method has a distance parameter, but that doesn't tell you whether it is in miles, or km, or light-years, and the return value doesn't tell you whether it is in tons or litres or gallons.
And all the methods can be implemented differently in each implementing class . . .

. . . but they all have start, stop, and useFuel methods which can be called wherever they are wanted.
 
Mustafa Garhi
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Campbell. Your examples are smooth steps towards a nice OO life

Could you please confirm the below understanding:

1. Considering your Car and Engine example, in real scenario the Developer (D) will make the Car related code plus the Engine interface and the User(U) has the responsibility to implement the Engine interface and send (y)our Car the engine he wants. And the place where ABSTRACTION comes into picture is while you(D) are developing the Car class you 'do not need to bother' what Engine could come to you. You just have to start/stop the Engine etc. And this is possible because of interfaces. Is all this understanding correct?

2. On the other hand you could have a method that returns the car Engine (the interface type) and user(U) could just use it 'without having to bother' anything about the implementation and use it like an Engine. This is ABSTRACTION for the user(U) of the code. Correct?

Regards
 
Akshay Sahu
Greenhorn
Posts: 26
Java Netbeans IDE
  • Likes 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Encapsulation: Encapsulation is defined as a process of wrapping methods and variables as a single entity or unit (whatever you prefer calling it ... )
In simpler terms:

Imagine around 1000 classes using the above class. Now what if I rename the variable value to integerValue, The code will surely not break because the only way to access the value of the variable value is via the method getValue and thus any changes made to the variable will not break the code. This thus defines encapsulation as a process of wrapping methods and variables as a single unit (I prefer calling it a UNIT).This actually is an example of Strong Encapsulation.

Now coming to
Abstraction: Abstraction is defined as a process of hiding implementation data from the usage area.

I have written Class A above and after its compilation I'll get a .class file (Please correct me if I am wrong !!!).I'll jar the .class file and will use the jar for application development.

After some years, I resign from my organization and a new employee joins and he is now been made to work on the same application. Somewhere in his code, he wants to use Class A's getValue() method. And without hesitating a bit, he imports Class A and calls its getValue() method.
This new developer is now using the getValue() method without actually knowing its implementation code.

Thus exemplifiring the concept of Abstraction.
Generally an API is used for Abstraction.
 
Jagannath Duraisamy
Greenhorn
Posts: 7
Eclipse IDE Hibernate Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After a long search for the understanding of abstraction .. your post made me very clear. a nice explanation everybody could understand. thanks
 
sivakumar komma
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abstraction:
“Showing the essential and hiding the non-Essential” is known as Abstraction.

Encapsulation:
The “Wrapping up of data and functions into a single unit” is known as Encapsulation.
Encapsulation is the term given to “the process of hiding the implementation details of the object”.
Once an object is encapsulated, its “implementation details are not immediately accessible any more”.
Instead they are packaged and are only indirectly accessed via the interface of the object.
 
Pavan Kumar Dittakavi
Ranch Hand
Posts: 106
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sivakumar komma wrote:
Encapsulation is the term given to “the process of hiding the implementation details of the object”.


Siva, don't you think that hiding the implementation details is Abstraction? [ Rather than Encapsulation that is ]. You click on the web browser and enter the url and hit Enter and voila..you have the website loaded. Now this is abstracting the details of its implementation [ headers/request/response ]from the user. => Abstraction.

Ilja Preuss's sixth post is priceless.

-Pavan.
 
sivakumar komma
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pavan Kumar Dittakavi wrote:
sivakumar komma wrote:
Encapsulation is the term given to “the process of hiding the implementation details of the object”.


Siva, don't you think that hiding the implementation details is Abstraction? [ Rather than Encapsulation that is ]. You click on the web browser and enter the url and hit Enter and voila..you have the website loaded. Now this is abstracting the details of its implementation [ headers/request/response ]from the user. => Abstraction.

Ilja Preuss's sixth post is priceless.

-Pavan.



hi pavan i completely agree with you what I said in my post is
for example client accessed website he want to see only GUI page not complete code
in my sense assential data is nothing but GUI ,nonessential data is nothing but code

Sivakumar
 
Dheeraj Bansal
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the difference is majorly of the intent

Intent behind the two are:-
Abstraction : Hiding the complexity
Encapsulation : Hiding in order to preserve the integrity of the data
 
Campbell Ritchie
Sheriff
Pie
Posts: 49847
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not convinced about those meanings.
Have a look at these Wikipedia links: 1 2 They are not that easy to understand quickly, I am afraid.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic