<p>Which class design is better and why?</p>

<pre><code>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;
}
</code></pre>

<p>OR</p>

<pre><code>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;
}
</code></pre>

[{'answer_id': 38827, 'author': 'Brad Wilson', 'author_id': 1554, 'author_profile': 'https://Stackoverflow.com/users/1554', 'pm_score': 4, 'selected': False, 'text': "<p>I don't like either one. What happens when someone is both a member and an employee?</p>\n"}, {'answer_id': 38829, 'author': '1800 INFORMATION', 'author_id': 3146, 'author_profile': 'https://Stackoverflow.com/users/3146', 'pm_score': 7, 'selected': True, 'text': '<p>The question is simply answered by recognising that inheritance models an "IS-A" relationship, while membership models a "HAS-A" relationship.</p>\n\n<ul>\n<li>An employee IS A user</li>\n<li>An employee HAS A userinfo</li>\n</ul>\n\n<p>Which one is correct? This is your answer.</p>\n'}, {'answer_id': 38834, 'author': 'Apocalisp', 'author_id': 3434, 'author_profile': 'https://Stackoverflow.com/users/3434', 'pm_score': 2, 'selected': False, 'text': '<p>Neither one is good. Too much mutable state. You should not be able to construct an instance of a class that is in an invalid or partially initialized state.</p>\n\n<p>That said, the second one is better because it favours composition over inheritance.</p>\n'}, {'answer_id': 38852, 'author': 'wilhelmtell', 'author_id': 456, 'author_profile': 'https://Stackoverflow.com/users/456', 'pm_score': 4, 'selected': False, 'text': '<p>Ask yourself the following:</p>\n\n<ul>\n<li>Do you want to model an Employee <em>IS</em> a User?  If so, chose inheritance.</li>\n<li>Do you want to model an Employee <em>HAS</em> a User information?  If so, use composition.</li>\n<li>Are virtual functions involved between the User (info) and the Employee?  If so, use inheritance.</li>\n<li>Can an Employee have multiple instances of User (info)?  If so, use composition.</li>\n<li>Does it make sense to assign an Employee object to a User (info) object?  If so, use inheritance.</li>\n</ul>\n\n<p>In general, strive to model the reality your program simulates, under the constraints of code complexity and required efficiency.</p>\n'}, {'answer_id': 38898, 'author': 'Jon Limjap', 'author_id': 372, 'author_profile': 'https://Stackoverflow.com/users/372', 'pm_score': 3, 'selected': False, 'text': '<p>The real questions are:</p>\n\n<ul>\n<li>What are the business rules and user stories behind a user? </li>\n<li>What are the business rules and user stories behind an employee? </li>\n<li>What are the business rules and user stories behind a member?</li>\n</ul>\n\n<p>These can be three completely unrelated entities or not, and that will determine whether your first or second design will work, or if another completely different design is in order.</p>\n'}, {'answer_id': 38905, 'author': 'liammclennan', 'author_id': 2785, 'author_profile': 'https://Stackoverflow.com/users/2785', 'pm_score': 4, 'selected': False, 'text': "<p>I don't think composition is always better than inheritance (just usually). If Employee and Member really are Users, and they are mutually exclusive, then the first design is better. Consider the scenario where you need to access the UserName of an Employee. Using the second design you would have: </p>\n\n<pre><code>myEmployee.UserInfo.UserName\n</code></pre>\n\n<p>which is bad (law of Demeter), so you would refactor to:</p>\n\n<pre><code>myEmployee.UserName\n</code></pre>\n\n<p>which requires a small method on Employee to delegate to the User object. All of which is avoided by the first design.</p>\n"}, {'answer_id': 38986, 'author': 'Gishu', 'author_id': 1695, 'author_profile': 'https://Stackoverflow.com/users/1695', 'pm_score': 2, 'selected': False, 'text': "<p>Stating your requirement/spec might help arrive at the 'best design'.<br>\nYour question is too 'subject-to-reader-interpretation' at the moment.</p>\n"}, {'answer_id': 38991, 'author': 'Jonathan', 'author_id': 1772, 'author_profile': 'https://Stackoverflow.com/users/1772', 'pm_score': 2, 'selected': False, 'text': "<p>Here's a scenario you should think about:</p>\n\n<p>Composition (the 2nd example) is preferable if the same User can be both an Employee and a Member. Why? Because for two instances (Employee and Member) that represent the same User, if User data changes, you don't have to update it in two places. Only the User instance contains all the User information, and only it has to be updated. Since both Employee and Member classes contain the same User instance, they will automatically both contain the updated information.</p>\n"}, {'answer_id': 51208, 'author': 'maccullt', 'author_id': 4945, 'author_profile': 'https://Stackoverflow.com/users/4945', 'pm_score': 4, 'selected': False, 'text': '<p>Nice question although to avoid distractions about <em>right</em> and <em>wrong</em> I\'d consider asking for the pros and cons of each approach -- I think that\'s what you meant by which is better or worse and why. Anyway ....</p>\n<h2>The First Approach aka Inheritance</h2>\n<p>Pros:</p>\n<ul>\n<li>Allows polymorphic behavior.</li>\n<li>Is <em>initially</em> simple and convenient.</li>\n</ul>\n<p>Cons:</p>\n<ul>\n<li><em>May</em> become complex or clumsy over time <em>if</em> more behavior and relations are added.</li>\n</ul>\n<h2>The Second Approach aka Composition</h2>\n<p>Pros:</p>\n<ul>\n<li>Maps well to non-oop scenarios like relational tables, structured programing, etc</li>\n<li>Is straightforward (if not necessarily convenient) to <em>incrementally</em> extend relations and behavior.</li>\n</ul>\n<p>Cons:</p>\n<ul>\n<li>No polymorphism therefore it\'s less convenient to use related information and behavior</li>\n</ul>\n<p>Lists like these + the questions <a href="https://stackoverflow.com/users/372/jon-limjap">Jon Limjap</a> mentioned will help you make decisions and get started -- then you can find what the <em>right</em> answers should have been ;-)</p>\n'}, {'answer_id': 13124792, 'author': 'supercat', 'author_id': 363751, 'author_profile': 'https://Stackoverflow.com/users/363751', 'pm_score': 0, 'selected': False, 'text': '<p>Three more options:</p>\n\n<ol>\n<li><p>Have the <code>User</code> class contain the supplemental information for both employees and members, with unused fields blank (the <code>ID</code> of a particular <code>User</code> would indicate whether the user was an employee, member, both, or whatever).</p></li>\n<li><p>Have an <code>User</code> class which contains a reference to an <code>ISupplementalInfo</code>, where <code>ISupplementalInfo</code> is inherited by <code>ISupplementalEmployeeInfo</code>, <code>ISupplementalMemberInfo</code>, etc.  Code which is applicable to all users could work with <code>User</code> class objects, and code which had a <code>User</code> reference could get access to a user\'s supplemental information, but this approach would avoid having to change <code>User</code> if different combinations of supplemental information are required in future.</p></li>\n<li><p>As above, but have the <code>User</code> class contain some kind of collection of <code>ISupplementalInfo</code>.  This approach would have the advantage of facilitating the run-time addition of properties to a user (e.g. because a <code>Member</code> got hired).  When using the previous approach, one would have to define different classes for different combinations of properties; turning a "member" into a "member+customer" would require different code from turning an "employee" into an "employee+customer".  The disadvantage of the latter approach is that it would make it harder to guard against redundant or inconsistent attributes (using something like a <code>Dictionary&lt;Type, ISupplementalInfo&gt;</code> to hold supplemental information could work, but would seem a little "bulky").</p></li>\n</ol>\n\n<p>I would tend to favor the second approach, in that it allows for future expansion better than would direct inheritance.  Working with a collection of objects rather than a single object might be slightly burdensome, but that approach may be better able than the others to handle changing requirements.</p>\n'}, {'answer_id': 25430916, 'author': 'Marcin Raczyński', 'author_id': 1763149, 'author_profile': 'https://Stackoverflow.com/users/1763149', 'pm_score': 3, 'selected': False, 'text': '<p>You can also think of Employee as a <strong>role</strong> of the User (Person). The role of a User can change in time (user can become unemployed) or User can have multiple roles at the same time. </p>\n\n<p>Inheritance is much better when there is <strong>real</strong> "is a" relation, for example Apple - Fruit. But be very careful: Circle - Ellipse is not real "is a" relation, because cirlce has less "freedom" than ellipse (circle is a <strong>state</strong> of ellipse) -  see: <a href="http://en.wikipedia.org/wiki/Circle-ellipse_problem" rel="noreferrer">Circle Ellipse problem</a>.</p>\n'}]
['https://Stackoverflow.com/questions/38820', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/191/']