This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

encapsulation..???

 
vijay mahendra
Ranch Hand
Posts: 51
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi one and all,

Real life example for encapsulation??

plzz give a reply .

thanks one and all.
 
Eric C Lee
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic