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?
Sharon SethConnect With a Mentor Commented:
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
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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.