File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes data encapsulation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "data encapsulation" Watch "data encapsulation" New topic

data encapsulation

anil gopal

Joined: May 26, 2003
Posts: 1
Could any one tell me whether there is any other way to achieve data encapsulation than making the attributes private?
Robert Z

Joined: Jan 18, 2001
Posts: 5
what did you have in mind.. ??
make your membervars private
and use getters and setters..

Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1090
Hi Anil
Well I guess thats it's not really neccessary to make a data member private for encapsulation. Default access and protected modifier should also do the work. It depends on how tightly encapsulated you want the data in your code to be.
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
Even where you only have package access, it still seems to me that you'd want tight encapsulation. Think of it from a user's point of view: what a pain it would be to have to wade through all the methods and fields to figure out what to do. It's much better to make the relevant methods and fields private, thus enabling the user to focus on what is important, ie what is accessible.
[ May 26, 2003: Message edited by: Roger Chung-Wee ]

SCJP 1.4, SCWCD 1.3, SCBCD 1.3
I agree. Here's the link:
subject: data encapsulation
jQuery in Action, 3rd edition