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; } 
                It means that you should put new code in new classes/modules. Existing code should be modified only for bug fixing. New classes can reuse existing code via inheritance. Open/closed principle is intended to mitigate risk when introducing new functionality.
Class Design Principles do not aim for code that works, but for code that can efficiently be worked on! A class (*) should have only one reason to change. Class PersonData is responsible for knowing the data of a person. Class CarFactory is responsible for creating Car objects.
The classic example of the inheritance technique causing problems is the circle-elipse problem (a.k.a the rectangle-square problem) which is a is a violation of the Liskov substitution principle. A good example here is that of a bird and a penguin; I will call this dove-penguin problem.
The question is simply answered by recognising that inheritance models an "IS-A" relationship, while membership models a "HAS-A" relationship.
Which one is correct? This is your answer.
I don't like either one. What happens when someone is both a member and an employee?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With