new clone feature

R

Roedy Green

There is supposed to be a way you can avoid the cast from clone.

What is the feature called and how do you use it.
There is apparently more to it that just saying

public Footprint clone()

instead of

public Object clone()
 
S

Stefan Ram

Roedy Green said:
What is the feature called and how do you use it.
There is apparently more to it that just saying
public Footprint clone()
instead of
public Object clone()

»Note that this definition supports covariant returns -
that is, the specialization of the return type to a
subtype (but only for reference types).«

Java Language Specification, Third Edition,
8.4.5 Method Return Type
 
P

Paul Tomblin

In a previous article said:
public Footprint clone()

Maybe it's because I come from a C++ background, but I've always preferred
to make a copy constructor rather that rely on clone(). So I would write
Footprint myClone = new Footprint(myOldFootprint);
rather than
Footprint myClone = (Footprint)myOldFootprint.clone();
 
P

Paul Tomblin

In a previous article, (e-mail address removed) (Paul Tomblin) said:
In a previous article, Roedy Green


Maybe it's because I come from a C++ background, but I've always preferred
to make a copy constructor rather that rely on clone(). So I would write
Footprint myClone = new Footprint(myOldFootprint);
rather than
Footprint myClone = (Footprint)myOldFootprint.clone();

I should mention that I'm aware of the usefulness of the clone situation
where the item you're cloning might be one of several subclasses of the
"Footprint" and you want the new one to be the right one.
 

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,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top