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.

2
  • 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). Commented Jul 2, 2012 at 7: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. Commented Jul 3, 2012 at 15:52

6 Answers 6

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.

Sign up to request clarification or add additional context in comments.

4 Comments

thanks TomTom for ur valuable advise
I was getting worried out of all these answers, there was no effective one... +1 for you!
Heh, then you COULD use inheritance with roles, if it looks right (they share some common methods for example).
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.
4

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

3 Comments

... or to put it another way: All staff and students must be Users, but not all users must be Staff and Students.
AH, no, sorry. Not for 20 years. No inheritance.
Oh sorry, u r right, thats my mistake, I'll change it now
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.

1 Comment

s u r right, am doing wrong, I'll change it
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
}

Comments

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.

Comments

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
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.