Quick question on polymorphism and instanceof

W

WillieLWZ

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
 
K

klynn47

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.
 
W

WillieLWZ

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)
 
C

Cyril

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
 
W

WillieLWZ

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);
}
}
 
J

Jeffrey Schwab

WillieLWZ said:
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.
 
A

Andrew McDonagh

WillieLWZ said:
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
 
W

WillieLWZ

Jeffrey said:
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.
 
W

WillieLWZ

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. "
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,979
Messages
2,570,185
Members
46,728
Latest member
FernMcmull

Latest Threads

Top