Help : Casting after a clone & method polymorphism

O

Osaman

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
 
A

Alex Molochnikov

----- Original Message -----
From: "Osaman" <[email protected]>
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
 
J

John C. Bollinger

Osaman said:
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
(e-mail address removed)
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top