Inheritence in C#

Multiple inheritence is not supproted by C#. But there is a solution to this using interfaces. Lets see how as explained by Ms. Asha Bora.

We know inheriting from a class is a powerful mechanism, but the real power of inheritance comes from inheriting from an interface.
Interface allows you to truly separate the "what" from "how". The interface tells what the name is, return type and parameter of the method are. Exactly how the method is implemented is not a concern of the interface. It represent how you want an object to be represent rather then how it happens to be implemented in a particular moment.

Syntax to declare interface: To declare the interface we use the interface keyword.
Eg:
interface IComparable
{
int xyz();
}


Points to be noted about Interface:
  • Interface should not contain any variable.
  • Interface should not have any constructor. Since a constructor contains the statements to initialize the variable of an object, but interface does not contain any variable.
  • A Destructor is also not allowed in an interface. Since it contain the statement to destroy the object instance.
  • No need to supply the access specifier to the methods. Since all the methods are public by default in an interface.
  • We cannot nest any types like enums , class, structs, interfaces, delegates inside an interface.
  • A class which implements an interface needs to implements all the members of the interface.

A class can inherit from a single class but can implement from multiple interfaces. An interface can inherit multiple interfaces. So we say that in C# multiple inheritance is applicable only through Interfaces, but not via classes.
A class or structure that implements an interface also implements the base interface of its inherited interface.
A class can extend another class and implement an interface at the same time. The base class is named first followed by a comma, followed by the interface.
eg:
class Defaultclass
{

}
class XYZ: Defaultclass, IComparable
{
...
}


Inheriting interface: we have declared an interface in the above example called IComparable, the below examples shows how a interface inherit another interface…
interface IOrder : IComparable
{
void abc();
}

6 comments:

Unknown said...

from where are you getting all this info?
now i feel like i don't know bit of C# tricks too..lol..any ways thanks, now onwards i will regularly visit your blog to check what other solutions are available in C#.

Ritesh Raghuvanshi said...

Thanks soniya for your valuable comments. It really fills joy in our team of authors.

We will keep this good work going and will gain your appreciation again and again.

Thanks. Keep Visiting.

Unknown said...

Thank you Ritesh...it was a great pleasure to get the feedback on the comments given by me..I will surely try to visit your blogspot regularly...

If possible can you provide some light on IpTV section??

Thank you...and wish to see such good information constantly on your blogspace by you and your team of authors.

Ritesh Raghuvanshi said...

Thanks a ton Soniya.

We are pleased to have a reader like you for our blog.

We would definately include the info about IPTV topic, rather we would also welcome some more suggestions from you.

Please do visit our website www.techyoddha.com

Let us know your eMail ID for constant communication.

Unknown said...

Dear Ritesh,

Many thanks...

I am glad that you accepted my suggestion...also you have asked my mail ID...I can provide you that but I don't want it to be leaked...and also I don't want any Spam from you or your teammates...

Ritesh Raghuvanshi said...

You be sure of the privacy of your email ID.

You can get my ID from my google profile.

Do not post ur id here in comments, just email us at techyoddha@gmail.com

Thanks and regards.