This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
Dear frnds, Till some days before, I was thinking that Encapsulation means hiding the data, means data hiding only. But some one said that it is diffrent. Is it different? If yes, then how? More questions on that..likeis Data hidng a part of OOP or encapsulation? Regards
Encapsulation in general is the process of enclosing one or more items in a container. The container, in OOP, is an object. The user accesses the information in the container through the interface of the object. The interface determines what information of the object the user can access (such as a public method) and what information is hidden for the user (such as a private method). This is data hiding. Thus, encapsulation can be a means to achieve data hiding, but not everything that is encapsulated is also hidden.
Till some days before, I was thinking that Encapsulation means hiding the data, means data hiding only. But some one said that it is diffrent. Is it different? If yes, then how? It's important to understand the reasoning behind encapsulation. It's not just hiding for the purpose of hiding. The real intent is to leverage on the weak coupling between the objects to write the code that is easy to extend. If you publish a class where everything is open (public), then the users of that class (other classes, that is), can not only access the internals of your published class, but also corrupt its state with the unintended usage. What's more, the users become dependent on the internal implementation of your class. If you ever decide to change the code in your published class (such as change the method signature, add a field, etc), all the user classes will need to change, too. In effect, an innocuous change in one place starts a chain reaction that may propagate through your entire system. Compare this with a well designed class whre everything is private except the user interface. The users of the class use the published public methods of the class and are oblivious to how exactly your published class is implemented. Should you decide to change the implementation of your class, this change is local to it, -- all the other classes that depend on your class do not need to change, because they still use the interface that you chose to publish. So while encapsulation is indeed hiding the data, it's also a concept with a foundation and reason.
Joined: Aug 25, 2003
Dear Jaap and Eugene, Thanks for your answers. So can I say that encapsulation in itself is not the data hinding. Encapsulation means enclosing(and not hiding) or just putting some items(fields and methods in our case) in a container(object in out case). Those items(fields and methods) which are private are hidden. And just these private items is called data hiding. I am correct in saying this? If not please correct me. Since C langugae have structures, but no private and public kinda thing, so can we say there is no data hinding there, but C has encapsulation?? Thanks and regards Sandeep Jindal
Sandeep, I think you got it! (I don't remember: can C structs encapsulate behaviour (functions)?)
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus