Inherit two classes. Is this possible?

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
 
P

Peter Duniho

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
 
A

Arne Vajhøj

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
 
S

shapper

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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top