Copy constructor for abstract class

Viewed 6866

I have an abstract class named AClass. In the same package I have AnotherClass, in which I have an ArrayList of AClass objects. In the copy constructor of AnotherClass I need to make a duplicate of AClass objects inside the ArrayList.

The problem:

I cannot create a copy constructor in AClass because it is an abstract class and I cannot know the name of the class which will inherit by AClass. Actually, in this project, no object will inherit from this class, but this project will be used as a library by other projects which will provide a class child of AClass. Is there an error in my design or is there a solution to this problem?

Edit: here's some code:

public class AnotherClass{
    private ArrayList<AClass> list;
...
    /** Copy constructor
    */
    public AnotherClass(AnotherClass another){
        // copy all fields from "another"
        this.list = new ArrayList<AClass>();
        for(int i = 0; i < another.list.size(); i++){
            // Option 1: this.list.add(new AClass(another.list.get(i)));
            // problem: cannot instantiate AClass as it is abstract
            // Option 2: this.list.add(another.list.get(i).someKindOfClone());
            // problem? I'm thinking about it, seems to be what dasblinkenlight is suggesting below
        }
    }
...
}
1 Answers
Related