Quick question on polymorphism and instanceof

Discussion in 'Java' started by WillieLWZ, Feb 22, 2006.

  1. WillieLWZ

    WillieLWZ Guest

    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
     
    WillieLWZ, Feb 22, 2006
    #1
    1. Advertising

  2. WillieLWZ

    Guest

    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.
     
    , Feb 22, 2006
    #2
    1. Advertising

  3. WillieLWZ

    WillieLWZ Guest

    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)
     
    WillieLWZ, Feb 22, 2006
    #3
  4. WillieLWZ

    Cyril Guest

    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
     
    Cyril, Feb 22, 2006
    #4
  5. WillieLWZ

    WillieLWZ Guest

    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);
    }
    }
     
    WillieLWZ, Feb 22, 2006
    #5
  6. 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.
     
    Jeffrey Schwab, Feb 22, 2006
    #6
  7. WillieLWZ wrote:
    > 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
     
    Andrew McDonagh, Feb 22, 2006
    #7
  8. WillieLWZ

    WillieLWZ Guest

    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.
     
    WillieLWZ, Feb 23, 2006
    #8
  9. WillieLWZ

    WillieLWZ Guest

    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. "
     
    WillieLWZ, Feb 23, 2006
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul Tomblin

    Re: Dumb instanceof question

    Paul Tomblin, Jul 11, 2003, in forum: Java
    Replies:
    35
    Views:
    1,233
    Manish Jethani
    Jul 27, 2003
  2. Dotty

    instanceof question

    Dotty, Feb 16, 2005, in forum: Java
    Replies:
    2
    Views:
    467
    dar7yl
    Feb 16, 2005
  3. JKop
    Replies:
    11
    Views:
    895
  4. Replies:
    21
    Views:
    21,588
  5. Krivenok Dmitry
    Replies:
    13
    Views:
    1,443
    Axter
    Jun 1, 2006
Loading...

Share This Page