Which class design is better and why?
public class User
{
public String UserName;
public String Password;
public String FirstName;
public String LastName;
}
public class Employee : User
{
public String EmployeeId;
public String EmployeeCode;
public String DepartmentId;
}
public class Member : User
{
public String MemberId;
public String JoinDate;
public String ExpiryDate;
}
OR
public class User
{
public String UserId;
public String UserName;
public String Password;
public String FirstName;
public String LastName;
}
public class Employee
{
public User UserInfo;
public String EmployeeId;
public String EmployeeCode;
public String DepartmentId;
}
public class Member
{
public User UserInfo;
public String MemberId;
public String JoinDate;
public String ExpiryDate;
}
User
,Employee
andMember
are all the roles in systems. However, the fields composing these classes are intended for and related with many other aspects such as identification, logging, naming and authorization, which are the contexts of usage. Roles can be performed jointly (inclusively) or exclusively. This feature can be expressed by association with the performer rather than via inheritance as no one role means another in common case as they can be performed independently. Inheritance is just a way of composing a class by including features into it. Thus, your question is incorrect. – Piedadpiedmont