Inherit two classes. Is this possible?

  • Thread starter Thread starter shapper
  • Start date Start date
S

shapper

Hello,

Is it possible to have a class that inherits two other classes? Or
just one?
I am not able to find a syntax of a class that inherits two other
classes to have the properties of those two classes.

Thanks,
Miguel
 
Hello,

Is it possible to have a class that inherits two other classes?

No. C# does not support "multiple inheritance" (that's the phrase you'll
want to Google if you want more details on the subject.

In some cases, you can accomplish an equivalent design by declaring
multiple interfaces and having a single class implement them. You can
have multiple interface implementations in a class, just not multiple
inheritance.

Another good alternative is to use composition. That's often preferable
to inheritance anyway, and when dealing with two disparate classes you
want to include in a single class, composition can be a much cleaner way
to do it.

Pete
 
shapper said:
Is it possible to have a class that inherits two other classes? Or
just one?
I am not able to find a syntax of a class that inherits two other
classes to have the properties of those two classes.

Not possible in C#.

You can only extend (inherit functionality) from one class, but
you can implement (inherit contract) from multiple interfaces.

Arne
 
No.  C# does not support "multiple inheritance" (that's the phrase you'll  
want to Google if you want more details on the subject.

In some cases, you can accomplish an equivalent design by declaring  
multiple interfaces and having a single class implement them.  You can  
have multiple interface implementations in a class, just not multiple  
inheritance.

Another good alternative is to use composition.  That's often preferable  
to inheritance anyway, and when dealing with two disparate classes you  
want to include in a single class, composition can be a much cleaner way  
to do it.

Pete

Thank You!
 
Back
Top