On Sunday 06 October 2002 09:57 pm, Michael G Schwern wrote:
> On Sun, Oct 06, 2002 at 01:49:26AM -0400, Noah White wrote:
> > >OTOH, Java interfaces have a loophole which is considered a design
> > >mistake.
> > >An interface can declare some parts of the interface optional and then
> > >implementors can decide if they want to implement it or not.  The
> > >upshot
> > >being that if you use a subclass in Java you can't rely on the optional
> > >parts being there.
> >
> > Say what?!? I don't think so. If a JAVA class which implements an
> > interface does not declare all of the methods of the interface then
> > that class will be abstract and you won't be able to instantiate a
> > concrete instance of it. You are guaranteed that any concrete instance
> > of a class which implements an interface will contain ALL methods
> > defined by the interface.
>

[snip]

>
> It is possible in Java to declare methods to be an "optional operation".
> This means the subclass may or may not implement that method and if you try
> to use an unimplemented method an UnsupportedOperationException is thrown.
>
> Effectively, this means that for every optional method you want to use,
> you've got to wrap it in a try block to catch the
> UnsupportedOperationException, recover and try to do something else if it's
> not there.  Makes using an optional operation complicated and error prone.

To be clear, UnsupportedOperationException is a subclass of RuntimeException, 
and therefore one is not required to wrap a try around methods that throw it.

>
> In effect, an optional interface is declaring "this method may or may not
> exist" which ain't that useful.
>
> I can't find the syntax or this or when it was introduced, might be 1.2,
> but I see it mentioned in the Collection API and tutorial docs:
>
> http://java.sun.com/docs/books/tutorial/collections/interfaces/collection.h
>tml
>
>     The Collection interface is shown below:
>     public interface Collection {
>         // Basic Operations
>         int size();
>         boolean isEmpty();
>         boolean contains(Object element);
>         boolean add(Object element);    // Optional
>         boolean remove(Object element); // Optional
>         Iterator iterator();
>

I think there may be some confusion here. In java, there's no special syntax 
to declare a method an optional part of the interface. All concrete classes 
that implement the Collection interface still must define full-bodied 
C<add(Object element)> methods. It just so happens that by convention some 
classes simply throw an UnsupportedOperationException, perhaps like so:

public class MyStaticCollection implements Collection {
  ...
  public boolean add (Object element) {
    throw new UnsupportedOperationException("add not supported");
  }
  ...
}

This is really no different than the following from Tie::Handle:

sub READ {
    my $pkg = ref $_[0];
    croak "$pkg doesn't define a READ method";
}

No special syntax, just convention enforced by the method implementations.

  - Dan Boorstein

Reply via email to