Article View: aus.computers.java
Article #233Re: Multiple interfaces
From: Coho (Richard Swain)
Date: Wed, 19 Jun 2002 22:12
Date: Wed, 19 Jun 2002 22:12
107 lines
4312 bytes
4312 bytes
Steve Horsley wrote:
> On Tue, 18 Jun 2002 19:40:17 +0100, "Coho (Richard Swain)"
> <vibron@krang.f9.co.uk> wrote:
>
>
>>Use Facets.
>>
>>interface Intf1{
>> public void X();
>>}
>>
>>interface Intf2{
>> public void X();
>>}
>>
>>class MyClass
>>{
>> private void intf2X()
>> {
>> // do interface 2 X() implementation
>> }
>>
>> private void intf1X()
>> {
>> // do interface 1 X() impementation
>> }
>>
>> Intf1 getIntf1()
>> {
>> return new Intf1()
>> {
>> public void X()
>> {
>> intf1X();
>> }
>> };
>> }
>>
>> Intf2 getIntf2()
>> {
>> return new Intf2()
>> {
>> public void X()
>> {
>> intf2X();
>> }
>> };
>> }
>>}
>>
>>Then you can use myclassinstance.getIntf2().X() or
>>myclassinstance.getIntf2().X() depending which you want to use/call.
>>>
>
> Hmm. I see the idea, but it's not exactly polymorphic, is it. You
> almost need to create a new interface:
>
> interface ThingThatDoesIntf1AndIntf2 {
> Intf1 getIntf1();
> intf2 getIntf2();
> }
>
> Except that you can't even pass this object round to a method that
> expects to be given an Intf1 or an Intf2.
But I don't see how using somekind of namespace resolution is
polymorphic either. i.e to specify which method to call you *have*
to specify the interface the method is on. IMHO getIntfN() does that
just as well as some kind of language trick.
You are right about specifying a new interface, but I would hope that
practically all concrete classes are expressions of interfaces - to
which those two methods can be added.
If a method takes a particular interface intf1 say you can always do
theMethod( myclass.getIntf1() );
and within that if you need to get hold of the other interface just do
Intf2 f2 = ((ThingThatDoesIntf1AndIntf2)fi).getIntf2();
For your last point. If a method takes an object of type intf1 *or*
intf2 I'm not quite sure how it suppose to know which X() is the one to
be called internally unless they they are functionally equivalent and
hence the same method, or unless the object being passed in is only an
implementation of one of the interfaces, in which case no specific
resolution is required.
Anyway, just to get it past the compiler you could change
ThingThatDoesIntf1AndIntf2 to
interface ThingThatDoesIntf1AndIntf2 implements intf1 and intf2
{...
If you still have doubts (maybe I am missing something) a concrete
example would be appreciated.
Rich.
> Steve
--
http://www.krang.f9.co.uk
Technical Details
Message-ID:
<3D1101BE.40506@krang.f9.co.uk>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!newsfeed.berkeley.edu!news-hog.berkeley.edu!ucberkeley!newshub.sdsu.edu!west.cox.net!cox.net!newspeer1-gui.server.ntli.net!ntli.net!news8-gui.server.ntli.net.POSTED!53ab2750!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>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: 10d6db,113f3357bc588129 X-Google-Attributes: gid10d6db,public X-Google-Thread: f74b3,113f3357bc588129 X-Google-Attributes: gidf74b3,public X-Google-ArrivalTime: 2002-06-19 15:12:01 PST Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!newsfeed.berkeley.edu!news-hog.berkeley.edu!ucberkeley!newshub.sdsu.edu!west.cox.net!cox.net!newspeer1-gui.server.ntli.net!ntli.net!news8-gui.server.ntli.net.POSTED!53ab2750!not-for-mail Message-ID: <3D1101BE.40506@krang.f9.co.uk> From: "Coho (Richard Swain)" <vibron@krang.f9.co.uk> User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.0.0) Gecko/20020530 X-Accept-Language: en,ru MIME-Version: 1.0 Newsgroups: aus.computers.java,borland.public.jbuilder.java.language,comp.lang.java.programmer Subject: Re: Multiple interfaces 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> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit Lines: 108 Date: Wed, 19 Jun 2002 23:12:14 +0100 NNTP-Posting-Host: 213.104.40.50 X-Complaints-To: abuse@ntlworld.com X-Trace: news8-gui.server.ntli.net 1024524718 213.104.40.50 (Wed, 19 Jun 2002 23:11:58 BST) NNTP-Posting-Date: Wed, 19 Jun 2002 23:11:58 BST Organization: ntlworld News Service