Say I have an abstract class called SuperClass and many classes extending it. I have an array of of type SuperClass[]. I want to make an array with new objects of the same subclasses and the same attributes.
I tried to do this by creating instantiating new objects and fill the new array with them. However this doesn't work as demonstrated as follows.
SuperClass[] newArray = new SuperClass[arr.length]; for (int i = 0; i <= arr.length; i++) { SuperClass toBeCopied = arr[i]; newArray[i] = new SuperClass(toBeCopied.attribute1, toBeCopied.attribute2...); This does not work because SuperClass is abstract and thus cannot be instantiated.
I also looked at .clone(), but SuperClass doesn't extend Cloneable.
Are there any other ways of making a copy of an array with unknown content types?
public abstract SuperClass copy()method toSuperClasswhich then needs to be implemented by every subclass