7

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.

1
  • 2
    What didn't work when you used super(clone)? Commented Nov 29, 2012 at 21:31

3 Answers 3

10

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 :(

Sign up to request clarification or add additional context in comments.

2 Comments

From this one "master" constructor principle coming? Any source reference? Would like to know who recommended it and in what context. Thanks
@nanosoft: I don't have a specific source, I'm afraid. (I wouldn't be surprised if it's in Effective Java though.) I wouldn't focus on who the source is though - I'd focus on the benefits, which tend to be a lack of duplication (which means you don't get inconsistencies through accidentally only changing one piece of code).
0

I would do:

 public Trainee (User clone) // By specifying `User` you allow the use in your code
 {
    super (clone);
    if (clone instanceof Trainee) {
      this.osia = clone.getOsia();
      this.dateNaiss = clone.getDateNaiss());
    }
 }

1 Comment

I'd avoid the instanceof operator, it ties you to a specific type.
0

A quick fix can be - just cast Trainee to User in super() call:

public Trainee (Trainee clone) {
    super ((User)clone);
    this (clone.getOsia(), clone.getDateNaiss());
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.