clone

G

Guest

I want to take the clone() of Cloneable objects which are upcasting to
Object

I give below a similar code:



class A {
public Object clone() {....}
....
}

class B {
public static int main(String[] argv) {
A a = new A();
Object aClone = getCloneForAnyObject(a);
}
public Object getCloneForAnyObject(Object a) { // Upcasting to Object
// if "a" is "Cloneable" I want to return "a.clone()"
// else I want to return "a".
// Can you implement this method?
}
}


Thanks!
 
P

Paul H. van Rossem

I want to take the clone() of Cloneable objects which are upcasting to
Object

I give below a similar code:



class A {
public Object clone() {....}
....
}

class B {
public static int main(String[] argv) {
A a = new A();
Object aClone = getCloneForAnyObject(a);
}
public Object getCloneForAnyObject(Object a) { // Upcasting to Object
// if "a" is "Cloneable" I want to return "a.clone()"
// else I want to return "a".
// Can you implement this method?
}
}


Thanks!

Is this what you mean? I only wonder why you would want to do something
like this; looks like a strange design contract. You might need to
reconsider your requirements vs. design...(?)

if (a instanceof Cloneable)
return a.clone();
else return a;

Paul.
 
G

Guest

I want to take the clone() of Cloneable objects which are upcasting to
Object

I give below a similar code:



class A {
public Object clone() {....}
....
}

class B {
public static int main(String[] argv) {
A a = new A();
Object aClone = getCloneForAnyObject(a);
}
public Object getCloneForAnyObject(Object a) { // Upcasting to Object
// if "a" is "Cloneable" I want to return "a.clone()"
// else I want to return "a".
// Can you implement this method?
}
}


Is this what you mean? I only wonder why you would want to do something
like this; looks like a strange design contract. You might need to
reconsider your requirements vs. design...(?)

for instance, if you store lots of objects in a "Vector", when you want
to clone an object from "Vector" you have only the "Object". Your
instance of a class is upcasted to "Object"

Now, you want to clone this object but you dont know the class type of
this object to downcast. What you can do?
if (a instanceof Cloneable)
return a.clone();
else return a;

this is wrong.
in Object clone() is protected
 
P

Paul H. van Rossem

I want to take the clone() of Cloneable objects which are upcasting
to Object

I give below a similar code:



class A {
public Object clone() {....}
....
}

class B {
public static int main(String[] argv) {
A a = new A();
Object aClone = getCloneForAnyObject(a);
}
public Object getCloneForAnyObject(Object a) { // Upcasting to Object
// if "a" is "Cloneable" I want to return "a.clone()"
// else I want to return "a".
// Can you implement this method?
}
}



Is this what you mean? I only wonder why you would want to do
something like this; looks like a strange design contract. You might
need to reconsider your requirements vs. design...(?)


for instance, if you store lots of objects in a "Vector", when you want
to clone an object from "Vector" you have only the "Object". Your
instance of a class is upcasted to "Object"

Now, you want to clone this object but you dont know the class type of
this object to downcast. What you can do?
if (a instanceof Cloneable)
return a.clone();
else return a;


this is wrong.
in Object clone() is protected

Indeed, that's why this is a wrong approach! If you have a vector of
Objects (say of ClassX1 and ClassX2), and those all derive from ClassX,
then you should provide a public clone() method for ClassX (possibly
abstract) and any of these subclasses (ClassX1.clone(), etc), thereby
overriding the ClassX and ultimately the Object's invisible clone(). So
you are able to clone() any object from your vector of ClassX, without
having to know (or test!) which type it is. This is called dynamic binding.
So you need an intermediate class. Not sure that this answers your question?

Paul.
 
O

Oscar kind

[Paul H. van Rossem wrote:]Now, you want to clone this object but you dont know the class type of
this object to downcast. What you can do?
if (a instanceof Cloneable)
return a.clone();
else return a;

this is wrong.
in Object clone() is protected

But an Object doesn't implement Cloneable, and thus this isn't attempted.

Of course, the cast in a.clone() was left as an exercise for the reader...

Add the cast, and you'll see that it works perfectly: the instanceof
operator tells you if the class can be cast to a certain type, so you know
you won't get a ClassCastException.
 

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,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top