• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Tightly encapsulated classes

 
Mathangi Shankar
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Doyle Matt
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

 
Mark Patrick
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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.
 
Srinath Vangari
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Tightly encapsulated classes have private fields & the methods should or sholud not be public .As it is not compulsory ,methods to be public.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Mathangi Shankar
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh yeah i forgot to put thr private keyword.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic