wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes encapsulation..??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "encapsulation..???" Watch "encapsulation..???" New topic
Author

encapsulation..???

vijay mahendra
Ranch Hand

Joined: Jan 01, 2007
Posts: 51
hi one and all,

Real life example for encapsulation??

plzz give a reply .

thanks one and all.
Eric C Lee
Greenhorn

Joined: Feb 22, 2007
Posts: 4
The most classical encapsulation example I can think of is Data Transfer Object(DTO).
DTO definition


One would try to hide information from the caller, so they don't know how you represent the data in the class, therefore, the field variables are all private.

Hope this helps.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Hmmmm. A better example of encapsulation is



All the data members of this class are encapsulated. No one has access to them outside of the class. Any computations using them are done inside the class.

getX() and setX() methods, more or less by definition, break encapsulation, because they expose the object's state directly. Yes, they can do error checking and whatnot, but the deeper issue is that a class with getters and setters is constrained in its implementation -- other classes know too much about it for it to change much.
[/code]


[Jess in Action][AskingGoodQuestions]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: encapsulation..???