Article View: aus.computers.java
Article #459Re: Subclasses in Java?
From: Martin Petschke
Date: Fri, 15 Sep 2000 00:00
Date: Fri, 15 Sep 2000 00:00
73 lines
2472 bytes
2472 bytes
You can do it like this :
processObject(Object obj)
{
if (obj instanceof A) {
A objA= (A)obj;
objA.writeA();
}
else
{
if (obj instanceof B){
B objB= (B)obj;
objB.writeB(); }
else
{
.....
}
}
}
but I would consider it a better design to have a common base class (or an interface) X for A, B, C .
Define in X a method write() and override it (or implement -if X is an interface)
in A,B,C with the same functions writeA, writeB, etc.
Then you can rewrite the function to
processObject(Object obj) {
if (obj instanceof X) obj.write()
else {
extra processing for obj;
}
}
Heikel schrieb:
> Hi,
>
> I ve the following problem:
>
> I have:
> 3 Classes: "A", "B" and "C"
> 1 Method: "processObject(Object obj)"
>
> "obj" may be from type "A", "B" or "C" .
>
> processObject(Object obj)
> {
> if (obj from type A)
> obj.writeA();
> else
> {
> if (obj from type B)
> obj.writeB();
> else
> {
> if (obj from type C)
> obj.writeB();
> else
> {
> extra processing for obj;
> }
> }
> }
> }
>
> How can I use the specific methods (writeA(), writeB(), writeC()) ?
> How should I change the code of "processObject(Object obj)"?
>
> Thank you
> H. Jrad :-)
Technical Details
Message-ID:
<39C1FA7E.83B7FCD3@bln.siemens.de>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<8psu6v$h73$1@papyrus.erlm.siemens.de>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10568c,6a0e5a05c1202005 X-Google-Attributes: gid10568c,public X-Google-Thread: 113ae9,6a0e5a05c1202005 X-Google-Attributes: gid113ae9,public X-Google-Thread: 10a146,6a0e5a05c1202005 X-Google-Attributes: gid10a146,public X-Google-Thread: 10d6db,6a0e5a05c1202005 X-Google-Attributes: gid10d6db,public X-Google-Thread: f66ae,6a0e5a05c1202005 X-Google-Attributes: gidf66ae,public From: Martin Petschke <Martin.Petschke@bln.siemens.de> Subject: Re: Subclasses in Java? Date: 2000/09/15 Message-ID: <39C1FA7E.83B7FCD3@bln.siemens.de> X-Deja-AN: 670089505 Content-Transfer-Encoding: 7bit References: <8psu6v$h73$1@papyrus.erlm.siemens.de> To: Heikel <haykeljrad@hotmail.com> X-Accept-Language: de,en,fr Content-Type: text/plain; charset=us-ascii Organization: SBS Mime-Version: 1.0 Newsgroups: aus.computers.java,comp.lang.java.help,comp.lang.java.programmer,de.comp.lang.java,siemens.comp.lang.java