Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2006

Tip: Looking for answers? Try searching our database.

Quick question on polymorphism and instanceof

Thread view: 
WillieLWZ - 22 Feb 2006 01:40 GMT
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
klynn47@comcast.net - 22 Feb 2006 02:29 GMT
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.
WillieLWZ - 22 Feb 2006 03:19 GMT
> 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)
Andrew McDonagh - 22 Feb 2006 19:39 GMT
>>You can avoid it by including an abstract method in the superclass that
>>matches each method in the subclass. Then through polymorphism the
[quoted text clipped - 4 lines]
> (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
WillieLWZ - 23 Feb 2006 06:56 GMT
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. "
Cyril - 22 Feb 2006 07:52 GMT
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
WillieLWZ - 22 Feb 2006 09:58 GMT
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);
   }
}
Jeffrey Schwab - 22 Feb 2006 13:32 GMT
> Please consider:
> ===================
[quoted text clipped - 31 lines]
> ====================
> 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.
WillieLWZ - 23 Feb 2006 06:42 GMT
> 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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.