0

Possible Duplicate:
c# multiple inheritance

Am just creating a simple application using Asp.net, here I have 3 classes User, Staff, Student. In the User class I've properties like UserName, Password, Email and Mobile and in the Staff and Student class General details like FirstName, MiddleName, LastName as well as Email, Mobile.

Here whenever I add a new Staff or Student, I've to create a user account for them.

so in the User class, I tried, inheriting the Staff class, so that I can access the properties in the User class from the Staff.

after adding the staff data successfully, I need to call the Add(I have this already) method in the user class which ll create a user account for the staff with his datas.

And one more thing, I know that C# doesn't support multiple inheritance, here I need to inherit both Staff and Student class. how can I do this.

Can anyone help me here.

Community
  • 1
  • 1
shanish
  • 1,964
  • 12
  • 35
  • 62
  • Am I getting you right that you want the User class to inherit from both Student and Staff? To my knowledge of object orientation principles this seems awkward. Normally a more specialized (Student or Staff) class should inherit from a more general (User). – mortb Jul 02 '12 at 07:00
  • @casperOne THis is closed wrongly - the question named as duplicate is theoretical and is correct, the example here is specific and IS NOT AN INHERITANCE QUESTION AS INHERITANCE IS NOT THE ANSWER HERE. As a result, information is lost when you close this, namely that you should solve this particular problem NOT using inheritance to start with, as this actually is an antipattern. Please reopen. – TomTom Jul 03 '12 at 15:52

6 Answers6

9

OK, nice to see SO many posts all from people that demonstrate why they should read a book on OO.

THere is NO inheritance involved here - this is a classical exampl where inheritance is na antipattern.

Instead, you should have a role based model.

User (or entity) has a collection of ROLES. Roles are / can be: staff, Student and whatever else.

This allows roles to be time limited, but also allows CHANGES In the role (student that later gets hired on staff). It allows to attach all relevant information to the role class instead of using up space on the User class.

It is not like that is SO uncommon - elements like windows file system have role based security for 10+ years. The usage of Inheritance to solve this problem (and open a dozen new problems) is a classical anti-pattern because it simply is the wrong approach.

The only thing worse is that you had User as subclass, instead of base class, but again: this is NOT a case for inheritance at all.

TomTom
  • 61,059
  • 10
  • 88
  • 148
  • thanks TomTom for ur valuable advise – shanish Jul 02 '12 at 07:10
  • I was getting worried out of all these answers, there was no effective one... +1 for you! – Polity Jul 02 '12 at 07:20
  • Heh, then you *COULD* use inheritance with roles, if it looks right (they share some common methods for example). – sventevit Jul 05 '12 at 20:39
  • Nope, anti-pattern. For example, ROLES CHANGE OVER TIME. So, a role collection can assign Role ID (employee number etc.) plus start and end. This is a classical anti-pattern - look good to the inexperienced, is strategically bad. – TomTom Jul 05 '12 at 20:54
4

Your "inheritance" is backwards - Staff and Student need to extend User, not the other way around.

Tieson T.
  • 20,774
  • 6
  • 77
  • 92
4

You are saying that whenever you add a new Staff or Student, a user account has to be created. So, why not make Staff and Student users rather than the other way round? i.e. have Staff and Student inherit from User.

O. R. Mapper
  • 20,083
  • 9
  • 69
  • 114
3

As others pointed out, you have inheritance backwards.

Structure should look like this:

class User
{
    // User members
}

class Staff : User
{ 
    //Staff members, User members inherited
}

class Student : User
{
    // Student members, User members inherited
}
Alex
  • 23,004
  • 4
  • 39
  • 73
1

Fo what you wanted to do use two interfaces instead. but they are right, your staff and student should inherit from user, because they are both users.

1
public abstract class User
{
    public string Username{get;set;}
    public string Password{get;set;}
    public string Email{get;set;}
    public string Mobile{get;set;}
    public string FirstName { get; set; }
    public string LastName { get; set; }
}
public class Student : User
{
    //other properties and methods
}
public class Staff : User
{
    //other properties and methods
}