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.
Bookmark this question. Show activity on this post. Derived class should inherit all ctors of base except the default ctor (it is explained here).
In C++ that statement makes a copy of the object's state. In Java it simply copies the reference. The object's state is not copied so implicitly calling the copy constructor makes no sense. And that's all there is to it really.
We can also use the clone() method to get a copy of an object from an existing object of the class. But the Copy constructor is better to use than the clone() method because of the following reasons: 1. It is easier to implement and use a copy constructor than the clone() method.
Generally, the copy constructor is a constructor which creates an object by initializing it with an object of the same class, which has been created previously. Java supports for copy constructors but unlike C language, Java does not provide an explicit copy constructor you need to define it yourself.
It would be better to make the "full" copy constructor for Trainee
also take a User
:
public Trainee(Trainee clone)
{
this(clone, clone.getOsai(), clone.getDateNaiss());
}
public Trainee(User clone, String osai, String dateNaiss)
{
super(clone);
this.osai = osai;
this.dateNaiss;
}
As far as possible, it's worth keeping to the pattern of having one "master" constructor in each class, which all other constructors chain to, directly or indirectly.
Now, it isn't clear whether it makes sense to create a Trainee
without specifying existing user information... or possibly specifying it in some other way. It may be that in this case you really do need to have two separate sets of constructors - one set for copy constructors, and one set for "just give me all the values individually" constructors. It really depends on your context - we can't tell just from this.
In that case, you would be breaking the "one master constructor" rule slightly, but you can think of there being two master constructors, one for each set of constructors for different purposes. Fundamentally, you're running into "inheritance gets messy" - which is all too common :(
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With