What is an Interface in C#.NET?


Interfaces are just like as abstract classes, but without implementation in it, means only signature of the members will allow in Interface. An abstract can allows both concrete and abstract members in it but Interface only allow abstract members in it.

An interface can contain only signature of classed, method, Indexers, delegates or events. The reason Interface only provide signature is they are inherited by classed, method, Indexers, delegates or events, which must provide the implementation for the members declared in the Index.
The key word Interface is used to create an interface. Below is the code for defining the sample interface.


interface IMySampleInterface
{
void MySampleMethod();
}

The following code demonstrates the Interface implementation. Below IMyValue interface define the signature for the properties for the val1 and val2, these properties need to set the get and set the properties. MyDerived class implemented these properties.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{

interface IMyValue
{

int val1
{
get;
set;
}

int val2
{
get;
set;
}
}

class MyDerived : IMyValue
{

private int _val1;
private int _val2;


public MyDerived(int x, int y)
{
_val1 = x;
_val2 = y;
}

// Property implementation:
public int val1
{
get
{
return _val1;
}

set
{
_val1 = value;
}
}

public int val2
{
get
{
return _val2;
}
set
{
_val2 = value;
}
}
}

class MainClass
{
static void MyPrintPoint(IMyValue p)
{
Console.WriteLine("x={0}, y={1}", p.val1, p.val2);
}

static void Main()
{
MyDerived p = new MyDerived(2, 3);
Console.Write("My Point: ");
PrintPoint(p);
}
}

}



Basically, C# does not support Multiple Inheritance, but we can achieve by using Interfaces. Below is the sample program to achieve the multiple inheritances in C#


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{

interface IScool
{
void Test();
}

interface IClass
{
void Test1();

}

public class Exam :IClass, IScool
{
void IScool.Test()
{
Console.WriteLine("This is the Scool TEst");
}
void IClass.Test1()
{
Console.WriteLine("This is the Class TEst");
}
}
class MainClass
{

static void Main()
{


}
}

}




Comments

Author: KUNDAN KUMAR SRIVASTAVA17 Feb 2012 Member Level: Gold   Points : 3

Interfaces are much like abstract classes and they share the fact that no instances of them can be created. However, interfaces are even more conceptual than abstract classes, since no method bodies are allowed at all. So an interface is kind of like an abstract class with nothing but abstract methods, and since there are no methods with actual code, there is no need for any fields. Properties are allowed though, as well as indexers and events. You can consider an interface as a contract - a class that implements it is required to implement all of the methods and properties. However, the most important difference is that while C# doesn't allow multiple inheritance, where classes inherit more than a single base class, it does in fact allow for implementation of multiple interfaces.

Author: SonyShiva17 Feb 2012 Member Level: Gold   Points : 1

In an interface class, all methods are abstract - there is no implementation so when we create an interface, we are creating a set of methods, properties without any implementation and it must be overridden by the implemented classes. The benefit is that it allows a class to be a part of two classes: one from inheritance hierarchy and one from the interface.

Author: PalaniKumar.A20 Feb 2012 Member Level: Gold   Points : 4

An interface is not a class. It is an entity that is defined by the word Interface. An interface has no implementation; it only has the signature or in other words, just the definition of the methods without the body. As one of the similarities to Abstract class, it is a contract that is used to define hierarchies for all subclasses or it defines specific set of methods and their arguments. The main difference between them is that a class can implement more than one interface but can only inherit from one abstract class. Since C# doesn’t support multiple inheritance, interfaces are used to implement multiple inheritance.


What is an Abstract class?
An abstract class is a special kind of class that cannot be instantiated. So the question is why we need a class that cannot be instantiated? An abstract class is only to be sub-classed (inherited from). In other words, it only allows other classes to inherit from it but cannot be instantiated. The advantage is that it enforces certain hierarchies for all the subclasses. In simple words, it is a kind of contract that forces all the subclasses to carry on the same hierarchies or standards.
Abstract class: This class has abstract methods (no body). This class cannot be instantiated. One needs to provide the implementation of the methods by overriding them in the derived class. No Multiple Inheritance.



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: