dynamic downcast

J

Joris Bleys

I was wondering if there was any possible way to dynamically downcast an
Object handle.

It has to be something like:

Iterator i = this.iterator();
while (i.hasNext()) {
Object o = i.next();
Class c = o.getClass();
//Insert DownCast here
}

Thx in advance !!!

George (not the Indian)
 
J

Joris Bleys

Let's say you could. What then? You are in the realm of Reflection
here. You could not write code directly that used any methods without
being clear on what type the object was.

I was thinkering about conditional deep cloning. I would call the clone()
method for each element in my container, leaving it up to the user of my
container to use nothing but objects that implement the Cloneable
interface. So it would not matter if they really are Cloneable or not.
 
T

Tor Iver Wilhelmsen

Joris Bleys said:
I was wondering if there was any possible way to dynamically downcast an
Object handle.

No, but you can use reflection.
Class c = o.getClass();
//Insert DownCast here

Downcast is irrelevant: As long as you don't know the actual type when
you write the code, what are you going to cast it to?

It looks like your problem is better solved by using an interface for
methods common to a set of objects, and cast to that interface.
 
J

Joris Bleys

No, but you can use reflection.


Downcast is irrelevant: As long as you don't know the actual type when
you write the code, what are you going to cast it to?

My intend was to use it for a deep cloning method in my containerclass. It
would call the clone() method for each element inside.
It looks like your problem is better solved by using an interface for
methods common to a set of objects, and cast to that interface.

Mmm, this might be a solution. I could try to cast it to the Cloneable
interface. I'll try this right away.

Thanx for the advice !!
 
T

Tor Iver Wilhelmsen

Joris Bleys said:
My intend was to use it for a deep cloning method in my containerclass. It
would call the clone() method for each element inside.

But clone() is in Object, so all you'd need to do was to check if was
instanceof Clonable.
 
J

Joris Bleys

But clone() is in Object, so all you'd need to do was to check if was
instanceof Clonable.

But Object.clone() is protected so I have to downcast it somehow (or is my
IDE fooling around with me ? (no it is not :s)).
 
T

Tor Iver Wilhelmsen

Joris Bleys said:
But Object.clone() is protected so I have to downcast it somehow (or
is my IDE fooling around with me ? (no it is not :s)).

Ah, yes - and the approach with using a custom method in an interface
is better anyway.
 
X

xarax

Joris Bleys said:
My intend was to use it for a deep cloning method in my containerclass. It
would call the clone() method for each element inside.


Mmm, this might be a solution. I could try to cast it to the Cloneable
interface. I'll try this right away.

Thanx for the advice !!

The Cloneable interface is just a marker, it has no
methods declared. The clone() method is protected.
Implementing the Cloneable interface does nothing
about the accessibility of the clone() method.

Unless a subclass has redefined with clone()
to be public access, your attempts will likely fail
miserably.

As others have said, your classes that are supposed
to know how to clone themselves should implement an
interface with the appropriate methods. You can
then use "instanceof" to decide if you can cast the
Object down to that interface, then use the method
to clone the underlying instance.

For this purpose, I use my own Replicatable interface.

public interface Replicatable
{
public abstract Replicatable replicate();
}

If I have a Fubar class that I want to be cloneable,
I do not implement Cloneable. I implement Replicatable
or a subinterface of Replicatable.

public interface ReplicatableFubar
extends Replicatable
{
public abstract Fubar replicateFubar();
}

public class Fubar
implements ReplicatableFubar
{
// implement the methods here
public ReplicateFubar replicateFubar()
{
return /* whatever you need to do to create a copy of this */;
}

public Replicate replicate()
{
return replicateFubar();
}
}

Now, in my code, when I have a known reference to a Fubar
object, I can use fubar.replicateFubar() to get a copy
of the Fubar object. When I only have a reference to
a Replicatable object, I can use replicatable.replicate()
to make another Replicatable object of the same underlying
type. This is polymorphism and it works very well.

The benefit of using a Replicatable interface is that
the underlying object decides what parts of its instance
will be replicated. Cloning can cause dangling references
and many other problems, if its not implemented exactly
right. So, to avoid confusion about what cloning is or
is not, I use Replicate.

Two cents worth. Your mileage may vary.
 
J

Joris Bleys

The benefit of using a Replicatable interface is that
the underlying object decides what parts of its instance
will be replicated. Cloning can cause dangling references
and many other problems, if its not implemented exactly
right. So, to avoid confusion about what cloning is or
is not, I use Replicate.

Two cents worth. Your mileage may vary.

Thanks, Xarax. This was exactly the same as what came up in my mind after
a couple of frustrating days of trying to use CloneAble. As a matter of
fact I was implementing this idea right now, and was hoping it would work
this way as expected.

You made me sure of what I am trying to do is right and beautiful. Many
(fictive) thank-cups are coming your way.

Joris

Xerox rox.
 

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

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top