Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

DataHiding and Encapsulation??

 
Sandeep Jindal
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jaap van Hengstum
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
John Smith
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sandeep Jindal
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sandeep, I think you got it!
(I don't remember: can C structs encapsulate behaviour (functions)?)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic