Encapsulation and abstraction


How is Encapsulation is different/similar from abstraction. Both seems almost same to me. when and where and how to use each one in the real world

Please advise
Any links resources ideas highly appreciated. Thanks in advance
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Sharon SethCommented:
They both  achieve the same thing , but the purpose is different
You intend to encapsulate something so that  you can expose what is supposed to be seen and hide what is not supposed to be seen. You want to hide something so that users using the encapsulated thing do not  try to use/manipulate that which you do intend them to use .
eg :You declare a class member as private , so that users extending the class will not be able to manipulate that member. As the owner of the class ,later on, you will be able to change the variable name whenever you want , without impacting the users  extending your class . So , encapsulation is  hiding for security and  for loose coupling between software modules/units. Encapsulation is achieved by packaging/encapsulating your unit as a Class

Abstraction is when you want to  hide something mainly because the user  need not know  the working details of that which he is using.
eg : When using JDBC , you get a connection simply by saying getConnection() . You never need to know  the details of how the connection is actually obtained  , the JDBC driver you are using at that time does everything for getting a connection. When you change your DB from say Oracle to Sybase , you simply change the JDBC driver and you don't need to change your code . After migrating to Sybase , you will still say getConnection() and Sybase implementation of JDBC(sybase jdbc driver) does the job of getting a connection for you . All through this process, you never need to know how the connection is obtained for oracle or how it is obtained for sybase .
So , abstraction is hiding for the purpose of hiding the underlying implementation complexities.This abstraction is achieved by using abstract classes and interfaces

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gudii9Author Commented:
I can relate well now EncaPsulation with Private
ABSTRACTion with ABSTRACT classes, interfaces and jdbc interface concept

>>They both  achieve the same thing , but the purpose is different

eventhough same thing does the path/way to achieve the purpose is same or different?

private methods could be abstract and vice versa right.

Practically can we use both encapsulation and abstraction together same time?

Please advise
Sharon SethCommented:
Encapsulation and abstraction go together - usig a class itself means encapsulation.
Now, these are OO concepts . You still have to read my answer and think about it more and more.

The purpose of making a method abstract is to let subclasses override and implement it . Making it private would not allow the subclasses to acces it .So, you can't have a private abstract method.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java EE

From novice to tech pro — start learning today.