GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Difference between data encapsulation and data abstraction Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Difference between data encapsulation and data abstraction" Watch "Difference between data encapsulation and data abstraction" New topic
Author

Difference between data encapsulation and data abstraction

Suresh KumarPandey
Ranch Hand

Joined: Sep 08, 2012
Posts: 46
what is the exact difference between data abstraction and encapsulation.

According to me using encapsulation we provide the users which are using my class to access the variables through a public method there by hiding the implementation details and if i would need to change anything in my methods i can do it woithout breaking others code.

Data abstraction is used for loose coupling so that we can make reusable codes.
Heena Agarwal
Ranch Hand

Joined: Dec 25, 2013
Posts: 261
    
    4
Suresh KumarPandey wrote:

According to me using encapsulation we provide the users which are using my class to access the variables through a public method there by hiding the implementation details and if i would need to change anything in my methods i can do it woithout breaking others code.

Data abstraction is used for loose coupling so that we can make reusable codes.


Both of those is what is meant by abstraction. Abstraction helps us deal with complexity. The caller of an API need not know how the API implements what it provides. For example, when we type System.out.println("some string"); it prints whatever we specify. We needn't ( and most often we shouldn't ) bother ourselves with how it does that printing.

Abstraction makes our life easy by hiding complexity and letting us focus on only those parts which are important for what we are dealing with.

Encapsulation is merely the wrapping up of data and methods that operate on this data together. Classes for example wrap together the data and the methods that operate on the data they contain. Encapsulation helps us wrap things that interact together in one logical, cohesive unit. This wrapping up of data and methods should be smart so that each wrapper is associated with a specific responsibility only. I think encapsulation is very similar to normalization ( the one we studied in databases ) in that respect.

Dan Drillich
Ranch Hand

Joined: Jul 09, 2001
Posts: 1180
Encapsulation is merely ...


For me seeing these two words side by side is difficult to digest. For anyone who programmed in C for years and had hundreds of methods and plenty of data structures floating around encapsulation is _huge_.

Regards,
Dan

William Butler Yeats: All life is a preparation for something that probably will never happen. Unless you make it happen.
Shubham Semwal
Ranch Hand

Joined: Mar 08, 2014
Posts: 62
Suresh KumarPandey wrote:what is the exact difference between data abstraction and encapsulation.
.


As far as i understand they go side by side. First you encapsulate and then it's abstracted.
Heena Agarwal
Ranch Hand

Joined: Dec 25, 2013
Posts: 261
    
    4
The way I look at it is encapsulation is an OOP concept, not really tied to any implementation, whether it is Java, Slang, C++ or C ( I included C cause many people consider that C supports true encapsulation - enough has been said on this - so I will not contribute more and also I can't be sure cause I haven't programmed in C for many years ), or any other programming language for that matter.

I think a well encapsulated wrapper protects data, i.e it provides data hiding, which is crucial for any object oriented implementation cause data is what constitutes the state of an instance. The goal is to not reduce the complexity, but to protect data.

Yes abstraction also hides details, i.e the implementation details. The goal here is, though, to reduce complexity. And it is the 'how' ( the implementation ) that is hidden, not 'what' as in the case of encapsulation.

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Difference between data encapsulation and data abstraction