For a project I have to create objects that represent different types of users. All types are subclasses inheriting from superclass User. In the example we have a Student, Teacher and a Coordinator. Every class has different implementations of certain methods. To know what kind of user we are dealing with, a record is pulled from a database, including a user role. Depending on the role, a different type of User subclass is created. Currently I am using a switch statement a seen below. However, since they all have identical constructors, this results in very repetitive code. How could this be done more elegant/efficiently?
public void makeUserTypes() {
switch (idRole) {
case 0:
user = new Student(idUser, username, firstName, lastName);
break;
case 1:
user = new Teacher(idUser, username, firstName, lastName);
break;
case 2:
user = new Coordinator(idUser, username, firstName, lastName);
break;
}
}