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.
This came up durning a talk at JAOO by Kevlin Henny entitled "Minimalism: A Practical Guide to Writing Less Code" which is a nice talk, Java or not. http://www.two-sdg.demon.co.uk/curbralan/papers.html Slide 7, which is about obeying contracts, says: Subclassing and implemented interfaces should follow substitutability Unsupported operations or operations that do a little less or expect a little extra add complexity and he went off on a short tangent during the talk about optional operations in Java. 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. 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.html 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(); http://java.sun.com/docs/books/tutorial/collections/interfaces/ To keep the number of core collection interfaces manageable, the JDK doesn't provide separate interfaces for each variant of each collection type. (Among the possible variants are immutable, fixed-size, and append-only.) Instead, the modification operations in each interface are designated optional: a given implementation may not support some of these operations. If an unsupported operation is invoked, a collection throws an UnsupportedOperationException . Implementations are responsible for documenting which of the optional operations they support. All of the JDK's general purpose implementations support all of the optional operations. http://java.sun.com/j2se/1.4.1/docs/api/java/util/Collection.html Method Summary boolean add(Object o) Ensures that this collection contains the specified element (optional operation). boolean addAll(Collection c) Adds all of the elements in the specified collection to this collection (optional operation). void clear() Removes all of the elements from this collection (optional operation). boolean contains(Object o) Returns true if this collection contains the specified element. I don't know basic Java syntax worth a damn, but I know its dirty little secrets. ;) -- Michael G. Schwern <[EMAIL PROTECTED]> http://www.pobox.com/~schwern/ Perl Quality Assurance <[EMAIL PROTECTED]> Kwalitee Is Job One It's Absinthe time!