Inheritance - help please?

  • Thread starter Thread starter Timothy V
  • Start date Start date
T

Timothy V

Hi,
I'm new to C# but i know a little of C++. My problem is inheritance within
c#, im not sure how to do it. For example, i know that a class can only
inherit one base class. But what if i want to do this:

User
Admin : User
Instructor : User
Student : User
HybridUser : Admin, Instructor, Student

How do i declare/create the Hybrid user within C#?

Thank you in advance,

Timothy.
 
Timothy V said:
I'm new to C# but i know a little of C++. My problem is inheritance within
c#, im not sure how to do it. For example, i know that a class can only
inherit one base class. But what if i want to do this:

User
Admin : User
Instructor : User
Student : User
HybridUser : Admin, Instructor, Student

How do i declare/create the Hybrid user within C#?

You can't - as you've said, you can only inherit from one base class.
You can, however, implement multiple interfaces, if that helps. You
might like to consider using aggregation instead - have the HybridUser
class have references to Admin, Instructor and Student instances, and
have properties which allow access those instances.

Without more detail of what you're really trying to do, it's hard to
know exactly what the best solution is.
 
...
I'm new to C# but i know a little of C++. My problem is
inheritance within c#, im not sure how to do it. For
example, i know that a class can only
inherit one base class. But what if i want to do this:

User
Admin : User
Instructor : User
Student : User
HybridUser : Admin, Instructor, Student

How do i declare/create the Hybrid user within C#?

There's several solutions to this, one is through aggregation:

interface IUser {}
interface IAdmin : IUser{}
interface IStudent : IUser{}
class Admin : IUser, IAdmin {}
class Student : IUser, IStudent {}
class HybridUser : IUser, IAdmin, IStudent
{
Admin a;
Student s;

// Method calls passed to the
// private instances
}

Another is to create roles for the user:

abstract class Role {}
class Admin : Role {}
class Student : Role {}

class User
{
ArrayList roles; // which contains the users roles
}

There are several others, but which one to choose is of course depending on
how the classes actually will fit into and will be used in your application.

// Bjorn A
 
Timothy V said:
Hi,
I'm new to C# but i know a little of C++. My problem is inheritance within
c#, im not sure how to do it. For example, i know that a class can only
inherit one base class. But what if i want to do this:

User
Admin : User
Instructor : User
Student : User
HybridUser : Admin, Instructor, Student

How do i declare/create the Hybrid user within C#?

Thank you in advance,

Timothy.

Hi Timothy

you are definitely thinking of multiple inheritance ! That is not possible
in C#.
However you can inherite from multiple interfaces.
Could you give some more details about the methods of each class ?

Fred
 
In your example Admin, Instructor and Stuent could all override methods of
the User base class - which of these overrides should be chosen for the
HybridUser class? (Note that this does not apply to interfaces, as they
don't contain an implementation)
That's the one of the major problems of multiple inheritance, and one of the
causes why it isn't possible in C#.
Personal suggestion: Build a class hierarchy for User-Groups:
UserGroup
Admin : UserGroup
Instructor : UserGroup
Student : UserGroup
And let each User object contain a list of UserGroups it belongs to.
That way you can enumerate what groups a user belongs to (not possible in
C++ without reflection), and decide for each method what should be done if
more than on UserGroup is in the list.

Niki
 
Back
Top