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 REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "encapsulation..???" Watch "encapsulation..???" New topic


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

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

Joined: Jul 08, 2003
Posts: 24195

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.

[Jess in Action][AskingGoodQuestions]
I agree. Here's the link:
subject: encapsulation..???
It's not a secret anymore!