1

I have a class 'Animal' and some subclasses of 'Animal' ('Cat', 'Dog', ...). Animal has a protected constructor and all the subclasses have a default constructor and a copy constructor.

At some point in the program I have a List of 'Animal' and I want to deep copy this list into another 'Animal' List (I mean to deep copy not only the List itself, but also all the objects in it).

To do so I wrote a function where you pass in the list to deep copy and it sets up a loop of all the elements of the list and, by using the copy constructor, adds to the new List all the elements of the old List.

The problem is that in order to call the right copy constructor I have to find out the type of 'Animal', via if... instanceof. This is not a big problem if I have few subclasses of 'Animal', but I'd prefer to create some copy constructors in the 'Animal' class which call the right subclass constructor based on the type of the argument I pass in.

But I read online that this is not possible. Is this true? And if so, is there another method apart from the if... instanceof method I'm using?

Lapo
  • 882
  • 1
  • 12
  • 28
  • 4
    Just add a copy() method that asks the animal to create and return a copy of itself, and override it in every subclass. Or make the classes immutable, so that creating a copy is generally useless. – JB Nizet Aug 11 '18 at 11:32
  • See this answers, hope it will help you =) https://stackoverflow.com/a/18146676/7136153 and https://stackoverflow.com/a/715901/7136153 – catscoolzhyk Aug 11 '18 at 12:21
  • @JBNizet thanks, I commented below the way I'm thinking of implement it. Is it right? Thanks a lot! – Lapo Aug 11 '18 at 12:46
  • @catscoolzhyk thanks, I liked the first answer. In the second I think it isn't the problem I'm facing. Moreover I read not to use clone() if possible because it has some problems. Thank you. – Lapo Aug 11 '18 at 12:48
  • 1
    @GhostCat you and JBNizet solved my problem, your is the answer to my problem. Moreover I think this is a common situation, so thank you two! – Lapo Aug 12 '18 at 14:12

1 Answers1

2

Your approach is wrong.

The whole idea of good OOP is that a superclass knows nothing about child classes. Oop is about being able to add more child classes without the need to touch any other parts of your code, including the parent class.

Rather have an abstract method deepCopy on your base class and have each child class implement that.

So that you later can clone animals without knowing their specific class!

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • So I create a method in 'Animal' which returns an Animal and has no arguments and I call it 'copy'. Then in 'Cat' I create another method which returns 'Animal' and has no arguments and I call it 'copy', so that it overrides the base class 'protected Animal copy()'. Then in this method body I write 'return new Cat(this)' and if I have a copy constructor in 'Cat' it works. Isn't it? – Lapo Aug 11 '18 at 12:44
  • @MarcoRossi that sounds right. Why don't you test your code? – JB Nizet Aug 11 '18 at 12:50
  • @JBNizet now I don't have my PC available. Moreover I prefer asking, because I'm pretty new to programming and I'm self learning, so I'd like to know if this is the best way, or if there are some side effects. As soon as I come back home I'll try this code. Thank you! – Lapo Aug 11 '18 at 13:09
  • 2
    Thing is : the most efficient way to learn programming is by trying and making experiments. You want to gather many experiences, instead of stopping after the first explanation! – GhostCat Aug 11 '18 at 13:19
  • @GhostCat You're right, I experiment a lot, but sometimes you don't understand if you're doing a thing in the best way if you don't ask people who are better than you or more experienced. Sometimes a method works but it isn't a good practice. Imho – Lapo Aug 12 '18 at 10:33
  • Sure, that is fine. It is about balancing own experiments and asking others to review / confirm your ideas. – GhostCat Aug 12 '18 at 13:55