It's not a secret anymore!*
The moose likes Beginning Java and the fly likes DataHiding and Encapsulation?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "DataHiding and Encapsulation??" Watch "DataHiding and Encapsulation??" New topic
Author

DataHiding and Encapsulation??

Sandeep Jindal
Ranch Hand

Joined: Aug 25, 2003
Posts: 180
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


SCJP 5.0
http://sites.google.com/site/duddlutechnologies/home
Jaap van Hengstum
Greenhorn

Joined: Jul 24, 2003
Posts: 24
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

Joined: Oct 08, 2001
Posts: 2937
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

Joined: Aug 25, 2003
Posts: 180
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

Joined: Jul 11, 2001
Posts: 14112
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
 
 
subject: DataHiding and Encapsulation??
 
Similar Threads
What is encapsulation
Encapsulation in java
Abstraction vs Encapsulation
Encapsulation
can we achieve data hiding withe the help of encapsulation...