Initializing parent variables in Child Class

Viewed 933

This is my parent class

abstract public class Person {
private String name;
private Date birthday;
private double difficulty;

protected abstract String personType();
protected abstract Person clone();

Person(String name, Date birthday, double difficulty) {
    this.name = name;
    this.birthday = birthday;
    this.difficulty = difficulty;
}

Person(Person copy) {
    this.name = copy.name;
    this.birthday = copy.birthday;
    this.difficulty = copy.difficulty;
}
Person(){
    this.name = "";
    this.birthday = new Date();
    this.difficulty = 0;
}

public String getName() {
    return this.name;
}

public Date getBirthday() {
    return this.birthday;
}

public double getDifficulty() {
    return this.difficulty;
}

And I want to make a subclass called Singer with the same constructor variables. My question is, how do I initialize the variables "name", "birthday", and "difficulty" in the Singer subclass by calling onto the parent class Person?

public class Singer extends Person{
String debutAlbum;
Date debutAlbumReleaseDate;

Singer(String name, Date birthday, double difficulty, String debutAlbum, Date debutAlbumReleaseDate){
    this.debutAlbum = debutAlbum;
    this.debutAlbumReleaseDate = debutAlbumReleaseDate;
    //im not sure what to put here for name, birthday, and difficulty
}   

}

1 Answers

You can use 1 of these 3:

  1. Using super() method (recommended): Add in the first line on the constructor this line super(name, birthday, difficult). This line will call the constructor of Person class for your object. Notice: super method can be used only in the first line of the constructor
  2. Add set() method: In the Person class, add set method for each variable and then call them in the constructor.
  3. Change access modifications: Change the access modifications of each variable in Person class to protected or public and then use this.name = name;.
Related