Help : Casting after a clone & method polymorphism

Discussion in 'Java' started by Osaman, Jan 4, 2004.

  1. Osaman

    Osaman Guest

    Hello

    I am having difficulty getting polymorphism to kick in as I thought it
    should
    after a clone. Here is the scenario.

    Let's say I have a base class "b" with derived classes "d1" and "d2".
    My method
    gets passed in an a parameter which tells me whether it is a d1 or a
    d2 (inputSelection) and an object (inputObject). So I end up with

    b tmp;
    switch(inputSelection){
    case 1:
    tmp = (d1) inputObject.clone();
    break;

    case 2:
    tmp = (d2) inputObject.clone();
    break;
    ....
    }
    tmp.methodFoo(arg1, arg2,...etc)

    the methodFoo that is being called here is the one declared inside
    class b,
    not the overridden one in classes d1 or d2. How can I get the
    appropriate
    methodFoo to be called ? Note that the only reason I was using the
    switch
    statement above was "to help" get tmp to be of the appropriate derived
    class.
    It would have been cleaner to even be able to just do
    tmp = (b) inputObject.clone();
    but I'm not sure if that will work or not...

    Thanks

    Osa
    Osaman, Jan 4, 2004
    #1
    1. Advertising

  2. ----- Original Message -----
    From: "Osaman" <>
    Newsgroups: comp.lang.java.programmer
    Sent: Sunday, January 04, 2004 10:30 AM
    Subject: Help : Casting after a clone & method polymorphism


    > Hello
    >
    > I am having difficulty getting polymorphism to kick in as I thought it
    > should
    > after a clone. Here is the scenario.
    >
    > Let's say I have a base class "b" with derived classes "d1" and "d2".
    > My method
    > gets passed in an a parameter which tells me whether it is a d1 or a
    > d2 (inputSelection) and an object (inputObject). So I end up with
    >
    > b tmp;
    > switch(inputSelection){
    > case 1:
    > tmp = (d1) inputObject.clone();
    > break;
    >
    > case 2:
    > tmp = (d2) inputObject.clone();
    > break;
    > ....
    > }
    > tmp.methodFoo(arg1, arg2,...etc)
    >
    > the methodFoo that is being called here is the one declared inside
    > class b,
    > not the overridden one in classes d1 or d2. How can I get the
    > appropriate
    > methodFoo to be called ? Note that the only reason I was using the
    > switch
    > statement above was "to help" get tmp to be of the appropriate derived
    > class.
    > It would have been cleaner to even be able to just do
    > tmp = (b) inputObject.clone();
    > but I'm not sure if that will work or not...
    >
    > Thanks
    >
    > Osa


    One way to find out whether or not this will work is by doing it.
    This aside, since the inputObject is a subclass of b, casting it to b is
    possible. This code should work:

    b tmp = (b) inputObject;
    tmp.methodFoo(arg1, arg2,...etc)

    BTW, according to the Java naming convention the class names should be
    capitalized.

    HTH

    Alex Molochnikov
    Gestalt Corporation
    www.gestalt.com
    Alex Molochnikov, Jan 4, 2004
    #2
    1. Advertising

  3. Osaman wrote:

    > I am having difficulty getting polymorphism to kick in as I thought it
    > should
    > after a clone. Here is the scenario.
    >
    > Let's say I have a base class "b" with derived classes "d1" and "d2".
    > My method
    > gets passed in an a parameter which tells me whether it is a d1 or a
    > d2 (inputSelection) and an object (inputObject). So I end up with
    >
    > b tmp;
    > switch(inputSelection){
    > case 1:
    > tmp = (d1) inputObject.clone();
    > break;
    >
    > case 2:
    > tmp = (d2) inputObject.clone();
    > break;
    > ....
    > }
    >
    > tmp.methodFoo(arg1, arg2,...etc)


    Your casts to specific subclasses of b are of little use. If classes d1
    and d2 are properly polymorphic then you should not have to treat them
    differently. I.e. all of the above could be reduced to:

    b tmp = (b) inputObject.clone();
    tmp.methodFoo(arg1, arg2, ...);

    The semantics are identical, because a cast does not change a reference
    or its referrent in any way -- it just explicitly declares that the
    runtime type check performed (if necessary) by the VM will be
    successful. If the compiler can determine that a particular reference
    cast is an upcast then it needn't generate any code for it at all.

    >
    > the methodFoo that is being called here is the one declared inside
    > class b,
    > not the overridden one in classes d1 or d2. How can I get the
    > appropriate
    > methodFoo to be called ? Note that the only reason I was using the
    > switch
    > statement above was "to help" get tmp to be of the appropriate derived
    > class.


    No help is needed. An object has only one class. Its references may be
    stored in variables of other (compatible) types but that does not change
    the object's behavior.

    > It would have been cleaner to even be able to just do
    > tmp = (b) inputObject.clone();
    > but I'm not sure if that will work or not...


    Have you tried it?


    If your program runs but tmp.methodFoo(...) is not selecting the
    subclasses' methods for dispatch, then the most likely problem is that
    methodFoo is static in class b. Static methods are not polymorphic in
    Java. That is, by the way, an excellent reason to adhere to the
    convention of never invoking them through object references.


    John Bollinger
    John C. Bollinger, Jan 5, 2004
    #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. kevin
    Replies:
    11
    Views:
    5,782
    Andrew McDonagh
    Jan 8, 2005
  2. lordy

    To clone or not to clone..

    lordy, Jul 7, 2006, in forum: Java
    Replies:
    3
    Views:
    707
    lordy
    Jul 7, 2006
  3. Krivenok Dmitry
    Replies:
    13
    Views:
    1,393
    Axter
    Jun 1, 2006
  4. Wally Barnes
    Replies:
    3
    Views:
    510
    Wally Barnes
    Nov 20, 2008
  5. Richard Trahan

    node object gets clobbered after clone

    Richard Trahan, Sep 5, 2004, in forum: Javascript
    Replies:
    1
    Views:
    99
    Lasse Reichstein Nielsen
    Sep 5, 2004
Loading...

Share This Page