Multiple constructors are required for Java inheritance

Asked 2 years ago, Updated 2 years ago, 135 views

I'm a self-taught student in Java. I am studying inheritance and super class now If a superclass can have multiple constructors, I would like to ask you how to create a subclass constructor. The code I wrote now works without errors, but I was wondering if there was a better way

//Person.java
public class Person{

    private String name;
    private int age;
    private String homeTown;


    publicPerson(String name){ // Set age and house to default if the creator has only a name
        this.name = name;
        this.age = 18;
        this.homeTown = "Seoul";
    }

    publicPerson(String name, age){ // If the creator only has a name and age, set the house to default
        this.name = name;
        this.age = age;
        this.homeTown = "Seoul";
    }

    public Person(String name, int age, String homeTown){
        this.name = name;
        this.age = age;
        this.homeTown = homeTown;   

I am a student who inherits a superclass named person

//student.java
public class Student extends Person{
    private double avgGPA;
    private int ID;
    private String[] classes;

    public Student(double avgGPA, int ID, String[] classes, String name){
        super(name);
        this.avgGPA = avgGPA;
        this.ID = ID;
        this.classes = classes;
    }

    public Student(double avgGPA, int ID, String[] classes, String name, int age){
        super(name, age);
        this.avgGPA = avgGPA;
        this.ID = ID;
        this.classes = classes;
    }

    public Student(double avgGPA, int ID, String[] classes, String name, int age, String homeTown){
        super(name, age, homeTown);
        this.avgGPA = avgGPA;
        this.ID = ID;
        this.classes = classes;

As you can see, I have to write 3 sangseongja for subclass Is there a way to use it in a simpler and easier way?

java constructor inheritance

2022-09-22 21:14

2 Answers

I don't think it can be helped that Person should have more than 3 constructors and Student should have 3 constructors in the first place. If you just need to reduce the code, it's possible as below:

public class Person {
    private String name;
    private Integer age;
    private String homeTown;

    public static final int DEFAULT_AGE = 18;
    public static final String DEFAULT_HOME_TOWN = "Seoul";

    publicPerson(String name){ // Set age and house to default if the creator has only a name
        this(name, null, null);
    }

    Public Person (String name, Integration) { // If the creator only has a name and age, set the house to default
        this(name, age, null);
    }

    public Person(String name, Integer age, String homeTown){
        this.name = name;
        this.age = age != null ? age : DEFAULT_AGE;
        this.homeTown = homeTown != null ? homeTown : DEFAULT_HOME_TOWN;
    }
}

public class Student extends Person {
    private double avgGPA;
    private int ID;
    private String[] classes;

    public Student(double avgGPA, int ID, String[] classes, String name) {
        this(avgGPA, ID, classes, name, null, null);
    }

    public Student(double avgGPA, int ID, String[] classes, String name, Integer age) {
        this(avgGPA, ID, classes, name, age, null);
    }

    public Student(double avgGPA, int ID, String[] classes, String name, Integer age, String homeTown) {
        super(name, age, homeTown);
        this.avgGPA = avgGPA;
        this.ID = ID;
        this.classes = classes;
    }
}

If you limit the above Student to one constructor, it will look like the following:

public class Student extends Person {
    private double avgGPA;
    private int ID;
    private String[] classes;

    public Student(double avgGPA, int ID, String[] classes, String name, Integer age, String homeTown) {
        super(name, age, homeTown);
        this.avgGPA = avgGPA;
        this.ID = ID;
        this.classes = classes;
    }
}

If this happens, the person who uses it will be uncomfortable. Always specify values for age and homeTown:

public class Test {
    public static void main(String[] args) {
        Student st = new Student(123, 456, new String[] { "A" }, "waldo", null, null);
    }
}


2022-09-22 21:14

I think it will be prettier if you use the Builder Pattern.

Search in Google


2022-09-22 21:14

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.