core classes still need to be declared final?

Martin Buchholz Martin.Buchholz at Sun.COM
Mon Jan 7 22:51:58 UTC 2008

Subclassability is a problem with "value-oriented" computing.

If security or extreme reliability is a concern, then
existing apis that took Integers or Strings as arguments would
have to make defensive copies on import or export, as they have
to do with arrays today.  Since existing classes depend on
the immutability of Integers and Strings, these must forever remain
non-subclassable, at least by untrusted application code.

Inheritance is the one cornerstone of object-oriented computing that
has disappointed us, now that we have gained experience with it,
since it seriously constrains the evolution of superclasses.
Prefer composition to inheritance.
Especially so with immutable "value" types.

For the particular case of range-restricted integers,
I have some sympathy.  It would be nice if the platform offered
such things.


Nick Radov wrote:
> Is it still necessary for the core Java classes such as
> java.lang.Integer to be declared final? I understand that may have been
> necessary in the early days for performance reasons, but modern JVMs no
> longer provide much of a performance benefit for final classes. For
> certain applications it would really be helpful to be able to subclass
> some of those core classes.
> For example, one application I'm working on deals with integer values
> that must be between 0 and 9999 inclusive. I would like to be able to
> create a custom Integer subclass which enforces that limit in the
> constructor, but currently that isn't possible. While I could create a
> new class that acts as a wrapper around Integer, the syntax would be
> much more awkward and that would also make it much more difficult to
> interface with other third-party classes.
> *Nick Radov | Research and Development Manager | Axolotl Corp*
> <>, d: 408.920.0800 x116, f:
> 408.920.0880
> 160 West Santa Clara St., Suite 1000, San Jose, CA, 95113

More information about the core-libs-dev mailing list