views:

566

answers:

5

I am trying to grab an object from an array list and copy it to another array list.

as per a request

public abstract class codeType{
    stuff
}

public class drawOval extends codeType{

}

main{
    arrayList<codeType> a
    arrayList<codeType> b

    a.add(new drawOval(stuff))

    a.add(new drawOval(other Stuff))

    b.add(a.get(0).clone) //or something like that

}

That does not work but I am looking for something like that.

A: 

Perhaps it's because you don't have parentheses in your clone call? Should be x.clone().

If that's not it, you need to be a lot more specific about what "dose not work" means.

Jeremy Huiskamp
+1  A: 

x.clone is not a function call. You need x.clone()

Also, there is no such thing as an Abstract Object in Java, your first line will not compile either.

Cloning is a fairly dangerous mechanism, by the way, but make sure you get the syntax first.

Uri
x.clone is sudo code as well as Abstract Object.
Jason
Even if he put in the parentheses it will not work, as his class does not have a public clone method.
newacct
A: 

Abstract classes don't produce "abstract objects". Abstract classes are classes with at least an abstract method, which is a method without body. When you extend an abstract class you are forced to implement those methods. Think of them just as a common API for your objects.

omgzor
A: 

You should serialize the object and then de-serialize it.

01
A: 

I assume by abstract object you mean the Object which is root of the class hierarchy,Object does not itself implement the interface Cloneable which will result in run time exception. could you post some code and detail on what you mean by does not work. As uri suggested its not a good idea to invoke clone if you are in doubt.avoid clone.

prateek urmaliya