[jdom-interest] Query regarding the Parent/Child interfaces.

Alex Rosen arosen at novell.com
Wed Mar 17 09:01:47 PST 2004


(1) Interfaces do describe is-a relationships. However, their primary
purpose is not let you test whether an object is of a particular type
(by using instanceof). It's primarily so that you can call a method on
an object via an interface, and the right implementation will
automatically get called. In your example, if you had a reference to a
Fruit and called its isRipe() method, then the right implementation
would get called without having to check the type - if the Fruit
happened to be a Pineapple then Pineapple.isRipe() would automatically
be called, but if it was a Melon then Melon.isRipe() would automatically
be called. (This is called polymorphism.) There are times when
instanceof is useful or necessary, but you should try to minimize them.

(2) In JDOM, the Parent interface tells you whether the object could
potentially have children. Perhaps would more accurate name would be
PotentialParent or CanHaveChildren, but they aren't very pleasing names,
so we use Parent. If you want to find out if a Parent actually has
children, you should still call getChildren.size(). Similarly with
Child.

Alex 

>>> Simon Gauld <Simon.Gauld at three.co.uk> 3/17/2004 7:06:07 AM >>>
It's been a long, long time since I looked at the lists, so excuse me
if I'm
bringing up a dead topic however it seems relevant as #1 is about to
be
released.  Bear with me - controversial questions are ahead!:

Regarding the Parent/Child interfaces on the Element class -

- I have always understood that the use of interfaces within Java is
to
describe  relationships between objects in terms of Object A "is a" B. 
For
example, "a Pineapple is a Fruit".   

(Please excuse my OO 101) - 

this allows me as a programmer to express common functionality across
objects. 

Assuming I didn't misunderstand that part of my OO class, then this
means
that when combined with java's nice instanceof, I can now determine if
a
Pinnable "Is A" fruit, by saying  

if ( pineapple instanceof Fruit ) {
	.. and do pineapple related menu stuff


-- applying this to the JDOM world now, I can say

if ( myElt instaneof Parent ) {
   // 
	parentCount++;

(Okay, OO 101 is finished.)

-- except, this isn't a correct way of determining if a given Element
is
actually a Parent ("Parent" in my world means it has children, not it
can
have children.  For example, a loose model of myself might be

class simon implements person;		// note not class simon
implements
parent, child, person.  

because I ain't a parent.  I work out if I'm a parent by being asked -
"simon, do you have any children?" - at which point I say "No".  At
some
point I might say "Yes" in the future, meaning I am a parent.

Elements now look to me like parents , *always*.  If they have
children, if
they don't, they're parents.   Similarly the whole shebang applies to
the
Child interface - a new Element instance not attached to anything "is
a
Parent" and "is a Child".  It isn't, is it?.

Now I know that I can ask an Element if it is a parent by asking for a
count
of its children, however the model seems to be wrong.  When and why did
the
model change to have Parent and Child interfaces on the Element.   Was
it
because asking .getChildren().size() > 0 was deemed not enough?

.s


simon gauld
programmer, enabling services, 3
direct 01628 76 5443
mobile 07711 314 053
video  07782 246 649 
www.three.co.uk 


________________________________________________________________________

This e-mail message (including any attachment) is intended only for the
personal 
use of the recipient(s) named above. This message is confidential and
may be 
legally privileged.  If you are not an intended recipient, you may not
review, copy or 
distribute this message. If you have received this communication in
error, please notify 
us immediately by e-mail and delete the original message.

Any views or opinions expressed in this message are those of the author
only. 
Furthermore, this message (including any attachment) does not create
any legally 
binding rights or obligations whatsoever, which may only be created by
the exchange 
of hard copy documents signed by a duly authorised representative of
Hutchison 
3G UK Limited.
________________________________________________________________________

_______________________________________________
To control your jdom-interest membership:
http://lists.denveronline.net/mailman/options/jdom-interest/youraddr@yourhost.com



More information about the jdom-interest mailing list