Interfaces in C#.Net


In C#, if you wanted to do multiple inheritance like that in C++, its not allowed. However, there is a workaround or another way to do this, by creating a class interface. An interface can have methods, properties and indexers, but no implementation is allowed. Only in the class object that the interface is being inherited from is implementation allowed. So interfaces: ONLY DECLARATION, NO IMPLEMENTATION.

An interface is like an abstract class, when to use these over the other is covered here: http://stackoverflow.com/questions/761194/interface-vs-abstract-class-general-oo

Here’s what I’m talking about. Writing an interface is like writing an abstract base class. However, one rule that stands firm is: Interfaces by default are PUBLIC, while classes are internal and can be private, public, protected, sealed, partial, etc.

interface IRead
{
void Read();
}
interface IWrite
{
void Write();
}
class NotePad: IRead, IWrite //multiple inheritance
{
//Notepad class will implement the two methods, Read and Write inside the class
public void Write()
{
Console.WriteLine("This method writes on the console window, its inherited from IWrite");
}
public void Read()
{
Console.ReadLine();//keeps the console window open so we can read the message
}
}
class Program
{
static void Main()
{
NotePad n = new NotePad();
n.Write();
n.Read();

//you can also do this as well
IWrite w = new NotePad();
w.Write();
IRead r = new NotePad();
r.Read();
}
}

In Java, implementing an interface is simple in terms of syntax: class A implements InterfaceB is the same as class A: InterfaceB. For multiple inheritance in Java, the syntax goes like this: class A extends B implements InterfaceC.

 The following are some important things about interfaces:
  1. We cannot create an object of an interface, we can only create a reference or Interface Variable.
  2. An interface will have only abstract methods (method declaration).
  3. Interfaces supports Inheritance, Polymorphism (Overloading, Overriding (using the “new” keyword to hide the interface methods above)).
  4. We cannot create variables in an interface.
  5. Only Properties, Indexers, Methods and Events are allowed in an interface.
  6. A class can inherit multiple interfaces, which is also shown in the snapshot above.
  7. We cannot create any Access modifiers with Interface Members (like private, public, protected, internal, protected internal, virtual, override, static, abstract etc.)
  8. The new keyword is allowed in an interface.
  9. All methods of an interface must be defined in every derived class.
  10. An interface is good for small or medium level projects.

More information on commonly used Interfaces: IList, ICollection, IEnumerable, and List.

Advertisements

One thought on “Interfaces in C#.Net

  1. Pingback: Using the IComparable interface example C#.Net | Bits and Pieces of Code

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s