I'm facing this week a problem, so I decided to use an approach that I'm not sure is right. So I need your help to help me understand what I'm possibly doing wrong.
I have two types of users: Internal and outsourced, they have some attributes that differs one of the other. What I think to do was.
So I created a employee class, that aggregate all the common attributes
public abstract class Employee {
private String name;
private String address;
private String telephone;
private String cpf;
private Integer age;
A outsourced class that have 2 more attributes
public class Outsourced extends Employee {
private String companyName;
private Date endDate;}
And a Internal for internal users.
public class Internal extends Employee {}
Is it right to have this internal class or I should use the employee for the internals? have a class with no attributes seems to be wrong to me, but at the same time I think that use the employee class for internal user to generic, if one day we create a new kind of user we my have some problems.
Anyway thanks for any help :)
CodePudding user response:
It's absolutely right.
Firstly, you could want append some special logic or fields to Internal
later. staffNumber
e.g.. Existence of a separate class lets too easy deal it.
Secondly, you could want separated Internal
from Outsourced
instances in your code some time.
CodePudding user response:
It seems fine to me. With this approach, you have an easily extensible business model if one day you have the need to either add some common property to all types of Employees (by adding it to Employee
), add some new property to either Outsourced
or Internal
or even creating a new type of Employee (by extending the abstract Employee
). I would say you are good to go and continue your implementation.