permaculture playing cards*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Tightly encapsulated classes 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 "Tightly encapsulated classes" Watch "Tightly encapsulated classes" New topic
Author

Tightly encapsulated classes

Mathangi Shankar
Ranch Hand

Joined: Nov 01, 2004
Posts: 56
Hi,

What are tightly encapsulated classes? What are the conditions whereby we can say that a class is tightly encapsulated?

What benefits a tightly encapsulated class can provide?

Please reply....

Mathangi.


--------------------------------<br />SCJP1.4
Doyle Matt
Ranch Hand

Joined: Jul 05, 2004
Posts: 76
I guess a tightly encapsulated class is a class that has all the intance variables private and provides getter and setter (accessor and mutator) methods on it.

for example:



SCJP 1.4
Mark Patrick
Ranch Hand

Joined: Feb 22, 2004
Posts: 51


public class Example
{
private int x;

public void setX(int x)
{
this.x = x;
}

public int setX()
{
return x;
}
}



The code example had default access to variable x which means every class in the same package can access it directly. The description is correct, but the code would not have been considered tightly encapsulated.


Mark Patrick<br />SCJP 1.4
SRINATH VANGARI
Greenhorn

Joined: Oct 08, 2004
Posts: 14
Hi,
Tightly encapsulated classes have private fields & the methods should or sholud not be public .As it is not compulsory ,methods to be public.


srinath vangari
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Encapsulation is a form of data hiding, intended to protect data from external corruption. A tightly encapsulated class prevents direct public access to fields by making them private.

These field values might be returned or modified by public "accessor" and "mutator" methods, although these methods are not required for encapsulation. (Note that accessor methods are typically named "get...," while mutator methods are typically named "set..."). To help protect data, mutator methods can impose constraints on argument values, and throw IllegalArgumentExceptions.

If a class is not tightly encapsulated, then none of its subclasses are. A class that returns a reference to an internal, mutable object is not tightly encapsulated. However, a tightly encapsulated class may return a reference to an immutable object, or to a copy or clone of an internal object.

Encapsulation makes code easier to maintain by allowing changes to the data structure without changes to the public interface.
[ November 04, 2004: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Mathangi Shankar
Ranch Hand

Joined: Nov 01, 2004
Posts: 56
Thanks everybody for providing the information and thanks Marc for such a detailed information about the tightly encapsulated classes.

I have read the same in other sites too.

Thanks again.

Mathangi.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

That summary is from my own study notes for the SCJP 1.4 exam. I intend to create a new website and post all my notes there soon.
Doyle Matt
Ranch Hand

Joined: Jul 05, 2004
Posts: 76
oh yeah i forgot to put thr private keyword.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Tightly encapsulated classes