Article View: aus.computers.java
Article #243Re: Multiple interfaces
From: Tristan Reeves
Date: Sat, 22 Jun 2002 02:13
Date: Sat, 22 Jun 2002 02:13
59 lines
4085 bytes
4085 bytes
> > That's not too bad. Except that there's no way to get back from the
> > Intf1 to the original myClass object. I'm not sure you should ever
> > want to, but I still feel alienated by this somehow.
> >
>
> You could do this if you really wanted to do. What you'd have to do is
> enhance all your interfaces with an extra method:
>
> Object getContainingObject();
>
> and then implement it this way within the anonymous call:
>
> Intf1 getIntf1()
> {
> return new Intf1()
> {
> public void X()
> {
> intf1X();
> }
> public Object getContainingObject() {
> return <Name of Containing Class>.this;
> }
> };
> }
>
> Admittedly not as easy as in a direct implementation, but it seems to
work.
>
> Wayne
This is one of the main differences between having one class implement all
the interfaces, and having the class delegate some or all of the interfaces
to inner classes. This "dislocation" might have unfortunate effects. Not
only can't you "get back" to the object, but you also can't get to any of
the other *interfaces* that that object is supposed to implement.
As usual I can't think of a good concrete example of why this would be a
problem. I guess a problem could arise if some method took an object
parameter and expected that object to implement more than one interface (ie
to be of a certain co-type). The object itself might use "facets", but the
calling method won't know that. All it would know is "here is an object, and
I expect it to implement these interfaces. I'll do some typecasting to the
interfaces I need". Name-resolution of some sort would allow the *single*
object to implement all of the required interfaces.
This doesn't convince me, however, because it is much safer for that method
to state which interfaces it needs in its signature - unless, of course, it
is a third party method...
Basically I now think that the problems related to multiple interfaces are
more theoretical than real, and that you can find workarounds when you need
them. Still, it does annoy me that you would *have* to use such workarounds
in some cases.
By the way, does anyone know if there is a "base" interface type -
equivalent of Object? I don't believe there is, but just want to make sure.
Tristan.
Technical Details
Message-ID:
<1024711986.718420@web.apex.net.au>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!news1.optus.net.au!optus!news.mel.connect.com.au!news.can.connect.com.au!not-for-mailReferences:
<1024283082.192985@web.apex.net.au> <JScP8.111$Fv1.17660@newsread2.prod.itd.earthlink.net> <1024289793.45649@web.apex.net.au> <pan.2002.06.17.07.46.24.235390.777@databasix.com> <3D0F7E91.5010401@krang.f9.co.uk> <3d10edc6.1491361@news.virgin.net> <3D1101BE.40506@krang.f9.co.uk> <3d121553.1321895@news.virgin.net> <3D126A72.4123E49B@i.hate.spam>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10a146,113f3357bc588129 X-Google-Attributes: gid10a146,public X-Google-Thread: f74b3,113f3357bc588129 X-Google-Attributes: gidf74b3,public X-Google-Thread: 10d6db,113f3357bc588129 X-Google-Attributes: gid10d6db,public X-Google-ArrivalTime: 2002-06-21 19:13:11 PST Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!news1.optus.net.au!optus!news.mel.connect.com.au!news.can.connect.com.au!not-for-mail From: "Tristan Reeves" <moonshine@apex.net.au> Newsgroups: aus.computers.java,borland.public.jbuilder.java.language,comp.lang.java.programmer Subject: Re: Multiple interfaces Date: Sat, 22 Jun 2002 12:13:08 +1000 Organization: APEX Internet Lines: 62 Message-ID: <1024711986.718420@web.apex.net.au> References: <1024283082.192985@web.apex.net.au> <JScP8.111$Fv1.17660@newsread2.prod.itd.earthlink.net> <1024289793.45649@web.apex.net.au> <pan.2002.06.17.07.46.24.235390.777@databasix.com> <3D0F7E91.5010401@krang.f9.co.uk> <3d10edc6.1491361@news.virgin.net> <3D1101BE.40506@krang.f9.co.uk> <3d121553.1321895@news.virgin.net> <3D126A72.4123E49B@i.hate.spam> NNTP-Posting-Host: news.apex.net.au X-Trace: auwa.connect.com.au 1024711987 22453 210.9.132.20 (22 Jun 2002 02:13:07 GMT) X-Complaints-To: abuse@connect.com.au NNTP-Posting-Date: 22 Jun 2002 02:13:07 GMT X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2600.0000 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2600.0000 Cache-Post-Path: web.apex.net.au!unknown@210.9.133.181 X-Cache: nntpcache 2.3.3 (see http://www.nntpcache.org/)