inherited method invocation

Discussion in 'Java' started by John N., Oct 23, 2003.

  1. John N.

    John N. Guest

    I have the following problem in which my program chooses to invoke one
    of several competing methods, the chosen one not being the one I want.
    Does anyone know whay?

    I have:

    interface A;
    class ASub implements A;

    interface B {
    foo(A myA);
    }

    class BSub implements B {
    foo(A myA);
    foo(ASub myASub);
    }

    class C {
    A myA;
    B myB;

    C(A myA, B myB) {
    this.myA = myA;
    this.myB = myB;
    }

    bar() {
    myB.foo(myA);
    }
    }

    main() {
    ASub myASub;
    BSub myBSub;
    C myC = new C(myASub, myBSub);

    C.bar();
    }


    Now, for some reason, bar() invokes BSub.foo(A myA) instead of
    BSub.foo(ASub myASub). Can someone explain why? And perhaps suggest a
    solution which is elegant (which i realize might involve rethinking
    everything).

    Thanks.
    John
     
    John N., Oct 23, 2003
    #1
    1. Advertising

  2. John N.

    Daniel Dyer Guest

    On 23 Oct 2003 11:55:21 -0700, John N. <>
    wrote:

    > Now, for some reason, bar() invokes BSub.foo(A myA) instead of
    > BSub.foo(ASub myASub). Can someone explain why? And perhaps suggest a
    > solution which is elegant (which i realize might involve rethinking
    > everything).



    You would either need to define the attributes of C as being of the more
    specific type or you would need to perform an explicit cast in the bar()
    method. Neither approach is ideal because you lose the generality in the
    implementation of C. Reflection might solve this but that certainly does
    not satisfy your "elegant" criteria.

    Your example is extremely abstract so it's difficult to suggest a better
    solution without knowing what problem you are trying to solve. As a
    general rule I avoid writing methods that have signatures that differ only
    in the type of a parameter where one type is interchangable with the other
    (if you see what I mean, I'm sure there's a better way to phrase it). As
    your posting indicates, it can make the behaviour of the code less obvious.

    Dan.

    --
    Daniel Dyer
     
    Daniel Dyer, Oct 23, 2003
    #2
    1. Advertising

  3. John N. <> scribbled the following:
    > I have the following problem in which my program chooses to invoke one
    > of several competing methods, the chosen one not being the one I want.
    > Does anyone know whay?


    > I have:


    > interface A;
    > class ASub implements A;


    > interface B {
    > foo(A myA);
    > }


    > class BSub implements B {
    > foo(A myA);
    > foo(ASub myASub);
    > }


    > class C {
    > A myA;
    > B myB;


    > C(A myA, B myB) {
    > this.myA = myA;
    > this.myB = myB;
    > }


    > bar() {
    > myB.foo(myA);
    > }
    > }


    > main() {
    > ASub myASub;
    > BSub myBSub;
    > C myC = new C(myASub, myBSub);


    > C.bar();
    > }



    > Now, for some reason, bar() invokes BSub.foo(A myA) instead of
    > BSub.foo(ASub myASub). Can someone explain why? And perhaps suggest a
    > solution which is elegant (which i realize might involve rethinking
    > everything).


    Method overloading with different parameters is not polymorphic. It's
    done entirely at compile time. Because the argument of foo() in bar()
    is of type "A", the compiler will compile it to call the foo(A)
    implementation, no matter what object that parameter refers to, be it
    a normal A, a ASub, or some other subclass of A.
    You'll have to do some sort of class checking with instanceof and then
    call the appropriate method depending on the result. Somewhat like
    this:

    if (myA instanceof ASub) {
    myB.foo((ASub)myA);
    /* cast needed to make compiler realise it's a different overloaded
    method */
    }
    else {
    myB.foo(myA);
    }

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "Products like that make me wish I could menstruate."
    - Andy Richter
     
    Joona I Palaste, Oct 23, 2003
    #3
    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. User
    Replies:
    9
    Views:
    10,434
  2. D. Lane
    Replies:
    0
    Views:
    773
    D. Lane
    Jun 27, 2003
  3. =?ISO-8859-1?Q?Daniel_Sj=F6blom?=

    Re: Method invocation conversion

    =?ISO-8859-1?Q?Daniel_Sj=F6blom?=, Nov 2, 2004, in forum: Java
    Replies:
    1
    Views:
    523
    Mark Bottomley
    Nov 2, 2004
  4. CliffMacGillivray
    Replies:
    7
    Views:
    709
    Roedy Green
    Sep 15, 2005
  5. 7stud --
    Replies:
    11
    Views:
    411
    7stud --
    Nov 9, 2007
Loading...

Share This Page