Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy constructor for abstract class

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
        }
    }
...
}
like image 565
HAL9000 Avatar asked Dec 30 '13 17:12

HAL9000


People also ask

Can abstract class have copy constructor?

Yes you should. Rules of having your own implementations for copy constructor, copy assignment operator and destructor for a Class will apply to even an Abstract Class.

Can an abstract class be copied?

Abstract Classes should be non copy-assignable.

What is the copy constructor of a class?

Copy constructor is used to initialize the members of a newly created object by copying the members of an already existing object. Copy constructor takes a reference to an object of the same class as an argument.

Is copy constructor in Java?

Like C++, Java also supports a copy constructor. But, unlike C++, Java doesn't create a default copy constructor if you don't write your own.


1 Answers

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

This is generally correct. However, since you have a list of AClass, you do not need to know the exact subtype: an abstract function that make a copy would be sufficient:

protected abstract AClass makeCopy();

This is similar to the clone() function of the java.lang.Object, except all subclasses must implement it, and the return type is required to be AClass.

Since each subclass knows its own type, they should have no problem implementing makeCopy() method. Here is how this would look in your code:

for (int i = 0 ; i < another.list.size() ; i++) {
    this.list.add(another.list.get(i).makeCopy());
}

Note: this design is known as the prototype pattern, sometimes informally called the "virtual constructor".

like image 145
Sergey Kalinichenko Avatar answered Oct 21 '22 10:10

Sergey Kalinichenko