Is it a good practice to provide an abstract method to help the developer to initialize a complex object in the superclass constructor?
Consider the following classes:
// Parent class
abstract public class A() {
protected Person[] people;
public A(Person[] people) {
this.people = people;
}
abstract protected Person[] initPeople();
}
// Child class
public class B extends A {
public B() {
super(initPeople());
}
@Override
protected Person[] initPeople() {
return new Person[] {
new Person("James"),
new Person("Mary"),
new Person("Elizabeth")
};
}
}
If there was no initPeople
method, the developer of the child class would probably add it himself because creating the people
object take several lines.
Is there a better way to do it? Thank you!