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

encapsulation..???

 
vijay mahendra
Ranch Hand
Posts: 51
  • 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
  • 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: 24211
35
Chrome Eclipse IDE Mac OS X
  • 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]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic