[1686] in java-interest
Re: extending string functionality.
daemon@ATHENA.MIT.EDU (Doug Lea)
Thu Sep 14 09:21:57 1995
Date: Thu, 14 Sep 1995 06:05:40 -0400
From: Doug Lea <dl@altair.cs.oswego.edu>
To: java-interest@java.sun.com
In-reply-to: <ac7caa8300021004f3b9@[129.46.110.41]> (psimpson@qualcomm.com)
As other people have mentioned, it's reasonably easy to build a class
(say, `XString') that's like String except it has more functionality
and/or different mechanics. (For example, use delegation.) But it's
not so easy to write client code that can accept EITHER a String or an
XString, without knowing or caring which. These two aspects often go
hand-in-hand.
It would be easier and more pleasant to write such client code if
every `final' class provided in the basic java packages were listed as
implementing an interface describing its operations (or at least a
coherent subset thereof). For example
interface StringIfc { // needs a better name
public int length();
public char charAt(int index);
...
}
final class String implements StringIfc {
...
}
class XString implements StringIfc {
...
}
This way, client code that requires this kind of polymorphism can step
up to using `StringIfc' for all types, trading a performance hit for the
added flexibility. Other clients that just list `String' for types
would be unaffected.
Note that you cannot add these kinds of interfaces in retrospect to
classes that you don't own. In Java, a class must itself claim in its
declaration that it implements an interface. So it would be a nice
service if future releases of Java listed interfaces in these cases.
While it seems extreme to require the Java coding convention that
EVERY java class must be declared to implement a defined interface
(which is the convention you need to adhere to when mapping CORBA IDL,
for example), it is a good rule of thumb for people creating packages
of basic, widely used functionality.
Class String is not the best example of all this, because of
the special relationship between this class and the compiler.
--
Doug Lea | dl@cs.oswego.edu | dl@cat.syr.edu | 315-341-2688 | FAX 315-341-5424
Computer Science Dept, SUNY Oswego, Oswego, NY 13126 | http://g.oswego.edu/dl/
Also: Software Eng. Lab, NY CASE Center, Syracuse Univ, Syracuse NY 13244
-
Note to Sun employees: this is an EXTERNAL mailing list!
Info: send 'help' to java-interest-request@java.sun.com