Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Quick question on polymorphism and instanceof

Reply
Thread Tools

Quick question on polymorphism and instanceof

 
 
WillieLWZ
Guest
Posts: n/a
 
      02-22-2006
Please consider:
===================
public class ClassA {
public void someMethod(ClassB objB) {
//...
}
}
===================
public class ClassB {
}
===================
public class SubClassB extends ClassB {
}
===================
public class Main {

private static class SubClassA extends ClassA {
public void someMethod(ClassB objB) {
if (objB instanceof SubClassB) { //<------ see question
below
//...
} else {
super.someMethod(objB);
}
}
}

private ClassA objA = new SubClassA();

public ClassA getClassA() {
return objA;
}
}
====================
Question: Can I avoid the use of the "instanceof" operator, if yes how?

With thanks,
Willie

 
Reply With Quote
 
 
 
 
klynn47@comcast.net
Guest
Posts: n/a
 
      02-22-2006
You can avoid it by including an abstract method in the superclass that
matches each method in the subclass. Then through polymorphism the
correct implementation will be used based on the run-time type of the
parameter.

 
Reply With Quote
 
 
 
 
WillieLWZ
Guest
Posts: n/a
 
      02-22-2006

http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> You can avoid it by including an abstract method in the superclass that
> matches each method in the subclass. Then through polymorphism the
> correct implementation will be used based on the run-time type of the
> parameter.


Thanks for the suggestion, however let's assume that the superclass
(ClassA) is in a library (say java.lang) and so cannot be modified and
that SubClassA is not to be exposed. (i.e. private class)

 
Reply With Quote
 
Cyril
Guest
Posts: n/a
 
      02-22-2006
Hi,

WillieLWZ a écrit :
> Question: Can I avoid the use of the "instanceof" operator, if yes how?


Move the method. The use of instanceof is usually the sign of a
misplaced method. In your case, you should call a method in ClassB and
redefine it in SubClassB.

Cheers,

Cyril

 
Reply With Quote
 
WillieLWZ
Guest
Posts: n/a
 
      02-22-2006
Thanks Cyril, would you provide an example?
Let's assume SubClassB already overrides all useful methods from
ClassB, and ClassA#someMethod(ClassB) performs some useful work on
instances of ClassB. The intent was for SubClassA to override
someMethod(ClassB) to deal with instances of SubClassB.

The following does not work:
=====================
private static class SubClassA extends ClassA {
public void someMethod(SubClassB objB) {
//...
}
public void someMethod(ClassB objB) {
//...
super.someMethod(objB);
}
}

 
Reply With Quote
 
Jeffrey Schwab
Guest
Posts: n/a
 
      02-22-2006
WillieLWZ wrote:
> Please consider:
> ===================
> public class ClassA {
> public void someMethod(ClassB objB) {
> //...
> }
> }
> ===================
> public class ClassB {
> }
> ===================
> public class SubClassB extends ClassB {
> }
> ===================
> public class Main {
>
> private static class SubClassA extends ClassA {
> public void someMethod(ClassB objB) {
> if (objB instanceof SubClassB) { //<------ see question
> below
> //...
> } else {
> super.someMethod(objB);
> }
> }
> }
>
> private ClassA objA = new SubClassA();
>
> public ClassA getClassA() {
> return objA;
> }
> }
> ====================
> Question: Can I avoid the use of the "instanceof" operator, if yes how?


Have SubClassA.someMethod() invoke a callback routine in objB that
includes all type-specific behavior. It is possible the objB.callBack()
will, in turn, invoke a method of A.

This means you will have to define the callback method in ClassB, and
override it in SubClassB.
 
Reply With Quote
 
Andrew McDonagh
Guest
Posts: n/a
 
      02-22-2006
WillieLWZ wrote:
> (E-Mail Removed) wrote:
>
>>You can avoid it by including an abstract method in the superclass that
>>matches each method in the subclass. Then through polymorphism the
>>correct implementation will be used based on the run-time type of the
>>parameter.

>
>
> Thanks for the suggestion, however let's assume that the superclass
> (ClassA) is in a library (say java.lang) and so cannot be modified and
> that SubClassA is not to be exposed. (i.e. private class)
>


This seems like a typical problem encountered hen inheritance is used
instead of delegation.

What would your design look like if you encapsulated the java.lang base
class, rather than derived from it?

For certain, the instanceof check would disappear.

Andrew
 
Reply With Quote
 
WillieLWZ
Guest
Posts: n/a
 
      02-23-2006

Jeffrey Schwab wrote:
>
> Have SubClassA.someMethod() invoke a callback routine in objB that
> includes all type-specific behavior. It is possible the objB.callBack()
> will, in turn, invoke a method of A.
>
> This means you will have to define the callback method in ClassB, and
> override it in SubClassB.


I apologize for leaving this out in the original question, assume
ClassA and ClassB are non-final classes in the java.lang library. No
method in ClassB acts on instances of ClassA.

 
Reply With Quote
 
WillieLWZ
Guest
Posts: n/a
 
      02-23-2006
Do you mean "Favor object composition over class inheritance"?

If so, I'm restricted by two criteria however:

1. Main#getClassA() is used by code to invoke ClassA#someMethod(objB)
2. ClassA does not implement an interface.

I'm stumped and would be thankful for an example.

>From Effective C++, by Scott Meyers :

"Anytime you find yourself writing code of the form "if the object is
of type T1, then do something, but if it's of type T2, then do
something else," slap yourself. "

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
instanceof NOT (always) bad? The instanceof myth. dmx_dawg@hotmail.com Java 21 07-20-2006 07:06 PM
Dynamic polymorphism vs. Static polymorphism Krivenok Dmitry C++ 13 06-01-2006 09:49 AM
instanceOf operator new bie question dbaplusplus@hotmail.com Java 14 05-02-2006 10:12 AM
instanceof question Dotty Java 2 02-16-2005 05:15 AM
Re: Dumb instanceof question Paul Tomblin Java 35 07-27-2003 07:31 PM



Advertisments