Rzeźnik said:
Lew, I am sorry to say this, but this does not make any sense. It goes
against the very spirit of OOP. If you write your published APIs in a
way you described then your APIs are useless in the face of design
changes. You can get away with this as long as you write programs for
yourself only, or programs with very short life cycle. Why do you fear
inheritance so much? I know of so called FBC problem, I am well aware
what it means, but it is no argument against inheritance.
I don't think anyone is arguing against inheritance in certain
situations. What matters though is the type of inheritance, and who it
is exposed to. Is it public inheritance or implementation inheritance?
Are we providing abstract classes or concrete classes to inherit from?
In another post I mentioned an app I help maintain where roughly a
quarter of about 2850 core classes are subclasses, or about 660 of them.
With the exception of about 30 custom exception classes (public
inheritance from Exception or subclasses thereof) and about 15 tag
handlers that inherit from Facelets TagHandler, and a tiny number of
other cases, all of the inheritance is internal to the application. Over
90 percent of it in fact. There aren't even five classes from external
libraries that we actually sub-class.
Point being, this is only a medium-sized J2EE app, but it's reasonably
complex. We use several dozen third-party APIs (libraries). And yet
we've seen the need to inherit from only 3 or 4 external classes. Based
on my experience this is not unusual.
IOW, inheritance (all sorts: implementation and public, concrete base
classes and abstract base classes) may get used a fair bit in the
_implementation_ of an application or a library, but that doesn't mean
that it gets used a lot across API boundaries. I certainly have never
seen that APIs expect a lot of it to occur, or that it's considered to
be desirable as a general rule.
Some exceptions to that observation are somewhat ubiquitous -
java.lang.Exception and subclasses as concrete base classes, or
javax.servlet.http.HttpServlet as an abstract base class. This simply
goes to show that _sometimes_ inheritance of a class in a public API is
a reasonable way to get things done.
Other posters have commented on this - there simply are better ways of
making your library extensible than to make it possible for clients to
subclass your classes willy-nilly. At most I can see the argument for
carefully crafted abstract base classes in certain situations - I see
considerably less use for non-final public concrete classes.
AHS