Using a class constructor to create object arrays of 2 different types

I'm implementing a database of sorts for a data structures project and i'm having a really hard time wrapping my head around the logic of what I need to do.

I have an abstract superclass, called Person, which has two child classes named Student and Instructor.

Lastly, I have another class called UniversityPeople which creates a reference to an array of both Student and Instructor objects when used by the application. When the constructor for UniversityPeople is called, it creates an array of the specified object using size as a parameter. I can't seem to think how to distinguish the two objects in the constructor at creation. My first thought was 2 constructors:

Instructor[] instArray;
Student[] stuArray;

public UniversityPeople(int size)
{
    Student[] stuArray = new Student[size];
}
public UniversityPeople(int size)
{
    Instructor[] instArray = new Instructor[size];
}

But after thinking about it(and doing some reading) I know I cannot do this. My next thought was to use a type of object validation method in the UniversityPeople constructor, but i'm having a hard time implementing one.

Basically, the class needs to know when to create an array of Student objects and when to create an array of Instructor objects with an integer size as an argument.

If anyone could point me in the right direction I would really appreciate it. I've been mostly coding in C lately so the return to OOP has felt a little weird after so much time. Thanks!

Jon Skeet
people
quotationmark

Firstly, I'd question the approach - it sounds like this single class is trying to do too many things. But if you really, really want to do it, I'd recommend static factory methods calling a private constructor:

public class UniversityPeople {
    private final Student[] students;
    private final Instructor[] instructors;

    private UniversityPeople(int studentCount, int instructorCount) {
        students = new Student[studentCount];
        instructors = new Instructor[instructorCount];
    }

    public static UniversityPeople forStudents(int size) {
        return new UniversityPeople(size, 0);
    }

    public static UniversityPeople forInstructors(int size) {
        return new UniversityPeople(0, size);
    }
}

You could perform a check against each size and only allocate if it's greater than 0 if you really want to. But as I say, I'd revisit the design if possible.

people

See more on this question at Stackoverflow