This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Svelte and Sapper in Action and have Mark Volkmann on-line!
See this thread for details.
Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

DataHiding and Encapsulation??

 
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
 
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.
 
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
 
author
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)?)
 
Get me the mayor's office! I need to tell him about this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic