After some searching I didn't found any good answer to my question regarding copy constructor and inheritance. I have two classes: User and Trainee. Trainee inherits from User and two String parameters are added to Trainee. Now I managed to make a copy constructor of User but I am not satisfied with my copy constructor of Trainee. The code of the User copy constructor is like this:
public User (User clone) {
this(clone.getId(),
clone.getCivilite(),
clone.getNom(),
clone.getPrenom(),
clone.getEmail(),
clone.getLogin(),
clone.getTel(),
clone.getPortable(),
clone.getInscription(),
clone.getPw()
);
}
I tried to use super in my Trainee copy constructor:
public Trainee (Trainee clone) {
super (clone);
this (clone.getOsia(), clone.getDateNaiss());
}
But it didn't work and I was forced to code a full version of the copy constructor:
public Trainee (Trainee clone) {
this(clone.getId(),
clone.getCivilite(),
clone.getNom(),
clone.getPrenom(),
clone.getEmail(),
clone.getLogin(),
clone.getTel(),
clone.getPortable(),
clone.getInscription(),
clone.getPw(),
clone.getOsia(),
clone.getDateNaiss()
);
}
Because of the construction of my main I have to cast my new instance like this:
User train = new Trainee();
User train2 = new Trainee((Trainee) train);
So my question is: Is there a cleaner way to do this? Can't I use a super?
Thank you in advance for your answers and assistance.