• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Tightly encapsulated classes

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oh yeah i forgot to put thr private keyword.
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic