Most of the books don't really clearly differentiate between Encapsulation, abstraction and Data Hiding and most of the time people treat these terms as synonyms.
When we ask people whts encapsulation ... the answer given is "Code is written in such a manner so that it acts as a capsule. And just like a capsule where we dont know whts inside it similarly we have code and we use it without knowing worrying about whts inside."
Now if we carefully analyse then above definition is mixture of all three terms and its not just encapsulation as is being told.
Data Hiding clearly means we need to hide data but then its our choice wht we need to hide and expose and thats where abstraction comes into picture.
Abstraction refers to deciding what are necessary details which we need to include in our code and expose[just like creating an ADT where mandatory operations are exposed but implementation is hidden]. But the implementation of those details needs to be hidden.
Now encapsulation provides us a placeholder [i.e a set of variables and operations] which is ultimately needed to implement Data Hiding and Abstraction.
Subscribe to:
Post Comments (Atom)
2 comments:
I think of encapsulation as follows:
In old old times, we had functional programming, there we treated data as separate entity and functions are separate entity.
In the new world, we allow combining both the data and function into a single entity (classes and objects). So this classes thing basically encapsulates both data and functions.
Regarding Abstraction, as per my understanding, we want to get an abstract picture of something without going into details. This is allowed very well by the classes (think about interfaces). For e.g. think about JDBC interfaces.
My 2 cents for data hiding... it seems to me that it is there just to have better control over the coupling of functions and data (else encapsulation will not be properly utilized). e.g. a public level permission will allow a very loosely coupled class where the data might be accessed by any tom, dick and harry (no offence to anyone with this name). In effect allowing you to bypass the "encapsulation" and write a functinoal programming in a world of objects.
Protected and private ensure that your functions in a class is a lot more tightly coupled with the data inside and in a way forces the programmer to use encapsulation.
In my mind I make sense... but do I?
"Data hiding would lead to encapsulation not being utilized properly ..."
Encapsulation is just a tool given to user ... how it is used is user's choice ..
I would rather put it as data hiding leads to data protection.
Post a Comment