Should added members be proteced or private?

Jeff Sturm jsturm@detroit.appnet.com
Fri Jul 28 19:26:00 GMT 2000


Mike Kienenberger wrote:
> > Should I keep everything that is not in the API spec as private,
> > or can I used friendly/default or protected as necessary?  I assume
> > I should not add anything public to the class.
> 
> One of my biggest gripes about Java is how difficult it is to subclass
> due to the restrictive nature of "private".   Final is another nasty
> impediment to subclassing.
> 
> I'd recommend "protected".   Otherwise, subclassing the object becomes
> problematic.

I disagree.  There's a reason for `private'.  Exposing instance methods
and especially variables needlessly contributes to the "fragile base
class" problem, in which minor changes to a base class can break those
which inherit from it.

More importantly, in the core classes variables/methods unspecified by
Sun's API must remain private, or libgcj will become incompatible. 
Otherwise the docs generated from libgcj wouldn't match Sun's, and
subclasses could become non-portable to other Java implementations.

Jeff


More information about the Java mailing list