[jdom-interest] TODO clone [eg]

Joseph Bowbeer jozart at csi.com
Sat Apr 14 14:01:15 PDT 2001


Dennis Sosnoski writes:

> Why force everyone to check the exception just in case someone
> at some point wants to implement a non-cloneable subclass?

My question is: Why implement Cloneable at all?  (Just checking...)

You obviously see "clone" as an important feature of JDOM classes and are
not interested in weakening that requirement.

> This seems to assume that subclassing will be the normal usage.
> I don't think that's true - I'm sure there'll be many cases where
> people subclass, but there'll probably be even more where they
> use the classes as is.

I understand your point of view, although this seems unworkable:

> I'd suggest the onus is on the developer implementing
> a non-clonable subclass to make sure it's not used by
> anything that expects to clone it

How can the developer exercise any control over what the user does?

The Liskov Substitution Principle (LSP) says that a subclass should work
anywhere a superclass does.  Unless the superclass declares the exception,
every subclass will be required to be cloneable -- or be in violation of
LSP.

Designing for subclassing is very hard.  Josh's hard-line advise for Java
Platform Libraries is to either design for inheritance or prohibit it.

We've decided to rely on inheritance as practically our sole means of
customization.  Consequently, we need to put a lot of thought into our
design.

In this case, I think we've put enough thought into it :-)

In which case it's time for plan B:

> > If we don't add the exception to the declaration, we
> > should at least throw an InternalError as a sanity check
> > in our own implementations:
> >
> >     public Object clone() {
> >         try {
> >             return super.clone();
> >         }
> >         catch (CloneNotSupportedException ex) {
> >             throw new InternalError();
> >         }
> >     }
> >

----- Original Message -----
From: "Dennis Sosnoski" <dms at sosnoski.com>
To: "Joseph Bowbeer" <jozart at csi.com>
Cc: <jdom-interest at jdom.org>
Sent: Saturday, April 14, 2001 1:11 PM
Subject: Re: [jdom-interest] TODO clone [eg]

Joseph Bowbeer wrote:

> The question for JDOM is whether clone is a requirement for subclasses or
> not.  By declaring the CloneNotSupportedException in the superclass, we're
> saying it's not a requirement and programmers should beware.  Another step
> back would be to only provide a "protected" clone method in the superclass
> and leave it up to subclasses to make it public if they want.

This seems to assume that subclassing will be the normal usage. I don't
think that's true - I'm sure there'll be many cases where people subclass,
but there'll probably be even more where they use the classes as is. Why
force everyone to check the exception just in case someone at some point
wants to implement a non-cloneable subclass?

I'd suggest the onus is on the developer implementing a non-clonable
subclass to make sure it's not used by anything that expects to clone it,
and if it is it's a program logic error - hence the
UnsupportedOperationException recommendation.

  - Dennis






More information about the jdom-interest mailing list