Article View: borland.public.jbuilder.java.api
Article #2030Re: When to use interfaces in OOP
From: Michael Rachow
Date: Wed, 27 Jun 2001 20:41
Date: Wed, 27 Jun 2001 20:41
26 lines
2097 bytes
2097 bytes
Hi Dick I came to Interfaces due to the following. In a more general class I had to store instances of classes with no common ancestor (except of course Object). So I used an Object to store them all. At this point for the compiler all these instances (of different classes) are Objects only and you are able to call methods of class Object only. You have to cast your instances to a more specialized type who knows the methods you want to call. So I had ask each instance by using instanceof are of type A or type B and so on, casted it to that type and were now able to call the method I needed. Then I realized that I wanted from all these instances nearly the same so they all implemented a method of the same name. But this doesn't help you at this point. Only if you let all your different classes implement an Interface that requirers that method to be defined you can throw away all these instanceof staments and cast all these different instances to that interface and call that method. You can even store now all these instances under that interface and have not to cast anymore. Ok a little bit bulky Michael
Technical Details
Message-ID:
<3b3a4478_2@dnews>Path:
arc1-ca.pugleaf.net!archiver1.google.com!newsfeed.google.com!sn-xit-02!supernews.com!nntp-relay.ihug.net!ihug.co.nz!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!nntp1.njy.teleglobe.net!teleglobe.net!hub.org!hub.org!PeerFeed.KWeb.on.ca!dnewsReferences:
<3b39c705_1@dnews> <3b39d697$1_1@dnews> <3b39ea6d_1@dnews>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 1089d0,a43ea4662650fd72 X-Google-Attributes: gid1089d0,public X-Google-ArrivalTime: 2001-06-27 13:41:52 PST From: "Michael Rachow" <michael@famrachow.de> Newsgroups: borland.public.jbuilder.java.api References: <3b39c705_1@dnews> <3b39d697$1_1@dnews> <3b39ea6d_1@dnews> Subject: Re: When to use interfaces in OOP Date: Wed, 27 Jun 2001 22:41:18 +0200 Lines: 28 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.00.2615.200 X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2615.200 NNTP-Posting-Host: 217.227.17.231 Message-ID: <3b3a4478_2@dnews> X-Trace: dnews 993674360 217.227.17.231 (27 Jun 2001 13:39:20 -0700) Path: arc1-ca.pugleaf.net!archiver1.google.com!newsfeed.google.com!sn-xit-02!supernews.com!nntp-relay.ihug.net!ihug.co.nz!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!nntp1.njy.teleglobe.net!teleglobe.net!hub.org!hub.org!PeerFeed.KWeb.on.ca!dnews