[635] in java-interest
Re: Categories in Java?
daemon@ATHENA.MIT.EDU (Mike Fleming)
Mon Jul 10 16:16:06 1995
From: Mike Fleming <mikef@xnet.com>
To: phil@bolthole.com (Philip Brown)
Date: Mon, 10 Jul 1995 14:58:37 -0500 (CDT)
Cc: java-interest@java.sun.com
In-Reply-To: <199507101843.LAA25584@shell1.best.com> from "Philip Brown" at Jul 10, 95 11:43:00 am
>
> >
> > A "category", for those who've never used Objective-C, is a group of methods
> > ...
>
> So now state how this is different from "interfaces".
>
I knew that someone was going to say this, but still I didn't make it clear
in my message. I'm sorry.
"Interfaces"--which are essentially identical to "protocols" in
Objective-C--declare a group of methods that classes can adhere to.
Interfaces only declare methods, they do not define an implementation.
Furthermore, a class wishing to adhere to a specific interface acknowledges
this in it's declaration.
A "category" not only declares a set of methods, it also defines
implementations for them (has code associated with them). A category
"adds" its methods/implementation to a specific class. A category
can be added/loaded at any time. A class doesn't acknowledge that
a category will be associated with it--the category states which
class it will be added to.
A category may, like a class, acknowledges that it conforms to an interface.
A category is not supposed to override methods that already are implemented
by a class. A category may not declare new instance variables, but it may
use existing ones.
To use my original example to completion, say one wanted to add a method
that would return a string value of an integer in a specified field with,
optionally padded with zeros on the left. Perhaps one would want to add
this method to the existing Integer class, for certainly that is where
such a method belongs. Of course, one does not want to directly edit
Sun's source. So, one creates a category of Integer that supplies this
additional method. This is what it might look like: (presumed java syntax)
category Integer moreStringMethods {
// ^^^^^^^ ^^^^^^^^^^^^^^^^^ ^^^
// | | |__Could have said "implements ..." here
// | |_This is the name of the category
// |________________This is the class which the category extends
public String toStringWidthZerofill (int field_width, boolean zerofill) {
<Code goes here>
}
}
Does anyone who has never seen categories beforte understand what I'm talking
about?
Mike
-
Note to Sun employees: this is an EXTERNAL mailing list!
Info: send 'help' to java-interest-request@java.sun.com