What is Interface?
- An interface can contain signatures (declarations) of the Methods, Properties, Indexers and Events.
- The implementation of the methods is done in the class that implements the interface.
- A Delegate is a type that can't be declared in an interface. You can either use an event (if appropriate) or declare a delegate outside the interface but in the same namespace.
- Interfaces in C# provides a way to achieve runtime polymorphism. Using interfaces, we can invoke functions from various classes through the same Interface reference, whereas using virtual functions we can invoke functions from various classes in the same inheritance hierarchy through the same reference.
- An interface can inherit from one or more base interfaces.
- A class that implements an interface can explicitly implement members of that interface.
- An explicitly implemented member cannot be accessed through a class instance, but only through an instance of the interface.
Purposes of Interfaces
- Create loosely coupled software.
- Support design by contract (an implementer must provide the entire interface).
- Allow for pluggable software.
- Allow objects to interact easily.
- Hide implementation details of classes from each other.
- Facilitate reuse of software.
Example 1
interface Iinterface_1
{
}
class Class1 : Iinterface_1
{
public void Display()
{
Console.WriteLine("Class1 Display Method.");
}
}
class Program
{
static void Main(string[] args)
{
Class1 objClass1 = new Class1();
objClass1.Display();
}
}
Output: Class1 Display Method
Example 2
The following is an example of Implicit and Explicit interface implementation:
interface Iinterface_1
{
void interface1_method();
}
class Class1 : Iinterface_1
{
public void Display()
{
Console.WriteLine("Class1 Display Method.");
}
//Implicit interface implementation
public void interface1_method()
{
Console.WriteLine("Iinterface_1 Method.");
}
//Explicit interface implementation
void Iinterface_1.interface1_method()
{
Console.WriteLine("Iinterface_1 Method.");
}
}
class Program
{
static void Main(string[] args)
{
Class1 objClass1 = new Class1();
objClass1.Display();
}
}
Output: Class1 Display Method.
Example 3
The following is an example of how to call an Implicit interface method:
interface Iinterface_1
{
void interface1_method();
}
class Class1 : Iinterface_1
{
public void Display()
{
Console.WriteLine("Class1 Display Method.");
}
//Implicit interface implementation
public void interface1_method()
{
Console.WriteLine("Iinterface_1 Method Implicit interface implementation.");
}
//Explicit interface implementation
void Iinterface_1.interface1_method()
{
Console.WriteLine("Iinterface_1 Method Explicit interface implementation.");
}
}
class Program
{
static void Main(string[] args)
{
Class1 objClass1 = new Class1();
objClass1.Display();
//Call Implicit interface method
objClass1.interface1_method();
Console.ReadLine();
}
}
Output: Class1 Display Method.
Iinterface_1 Method Implicit interface implementation.
Note:
![Implicit.jpg]()
In the picture above, when you type objClass1., then only interface1_method will be shown and Iinterface_1.interface1_method is not found.
So you can't call explicit interface from class object. (i.e objClass1)
Example 4
The following is an example of how to call an explicit interface method.
interface Iinterface_1
{
void interface1_method();
}
class Class1 : Iinterface_1
{
public void Display()
{
Console.WriteLine("Class1 Display Method.");
}
//Implicit interface implementation
public void interface1_method()
{
Console.WriteLine("Iinterface_1 Method Implicit interface implementation.");
}
//Explicit interface implementation
void Iinterface_1.interface1_method()
{
Console.WriteLine("Iinterface_1 Method Explicit interface implementation.");
}
}
class Program
{
static void Main(string[] args)
{
Class1 objClass1 = new Class1();
objClass1.Display();
//Call Implicit interface method
objClass1.interface1_method();
//Call Explicit interface method
Iinterface_1 obj_1 = new Class1();
obj_1.interface1_method();
Console.ReadLine();
}
}
Output: Class1 Display Method.
Iinterface_1 Method Implicit interface implementation.
Iinterface_1 Method Explicit interface implementation.
Example 5
The following is an example of how to call "Implicit Interface Method" in the same class using class method.
interface Iinterface_1
{
void interface1_method();
}
class Class1 : Iinterface_1
{
public void Display()
{
Console.WriteLine("Class1 Display Method.");
//Calling Implicit interface implementation
interface1_method();
}
//Implicit interface implementation
public void interface1_method()
{
Console.WriteLine("Iinterface_1 Method.");
}
//Explicit interface implementation
void Iinterface_1.interface1_method()
{
Console.WriteLine("Iinterface_1 Method.");
}
}
class Program
{
static void Main(string[] args)
{
Class1 objClass1 = new Class1();
objClass1.Display();
Console.ReadLine();
}
}
Output: Class1 Display Method.
Iinterface_1 Method.
Example 6
The following is an example of how to call "Explicit Interface Method" in the same class using class methods.
interface Iinterface_1
{
void interface1_method();
}
class Class1 : Iinterface_1
{
public void Display()
{
Console.WriteLine("Class1 Display Method.");
//Call Explicit interface method
Iinterface_1 obj_1 = new Class1();
obj_1.interface1_method();
}
//Implicit interface implementation
public void interface1_method()
{
Console.WriteLine("Iinterface_1 Method Implicit interface implementation.");
}
//Explicit interface implementation
void Iinterface_1.interface1_method()
{
Console.WriteLine("Iinterface_1 Method Explicit interface implementation.");
}
}
class Program
{
static void Main(string[] args)
{
Class1 objClass1 = new Class1();
objClass1.Display();
//Call Implicit interface method
objClass1.interface1_method();
Console.ReadLine();
}
}
Output: Class1 Display Method.
Iinterface_1 Method Explicit interface implementation.
Iinterface_1 Method Implicit interface implementation.
Example 7
The following is an example of how to call "Two interface methods" using a class object.
interface Iinterface_1
{
void interface1_method();
}
interface Iinterface_2
{
void interface2_method();
}
class Class1 : Iinterface_1, Iinterface_2
{
public void Display()
{
Console.WriteLine("Class1 Display Method.");
}
public void interface1_method()
{
Console.WriteLine("Iinterface_1 Method.");
}
public void interface2_method()
{
Console.WriteLine("Iinterface_2 Method.");
}
}
class Program
{
static void Main(string[] args)
{
Class1 objClass1 = new Class1();
objClass1.Display();
//Call Implicit interface method
objClass1.interface1_method();
objClass1.interface2_method();
}
}
Output: Class1 Display Method.
Iinterface_1 Method.
Iinterface_2 Method.
Example 8
The following is an example of how to call "Two interface methods" separately in a class.
interface Iinterface_1
{
void interface1_method();
}
interface Iinterface_2
{
void interface2_method();
}
class Class1 : Iinterface_1, Iinterface_2
{
public void Display()
{
Console.WriteLine("Class1 Display Method.");
}
public void interface1_method()
{
Console.WriteLine("Iinterface_1 Method.");
}
public void interface2_method()
{
Console.WriteLine("Iinterface_2 Method.");
}
}
class Program
{
static void Main(string[] args)
{
Iinterface_1 obj_1 = new Class1();
obj_1.interface1_method();
Iinterface_2 obj_2 = new Class1();
obj_2.interface2_method();
Console.ReadLine();
}
}
Output: Class1 Display Method.
Iinterface_1 Method.
Iinterface_2 Method.
Note:
1. If you try to call "Interface2_method()" with "obj_1" object, then it will give you a compile error, as in:
//Error
//obj_1.interface2_method(); //'Iinterface_1' does not contain a definition for 'interface2_method'
Example 9
The following is an example of Interface inheritance.
interface Iinterface_1
{
void interface1_method();
}
interface Iinterface_2 : Iinterface_1
{
void interface2_method();
}
class Class1 : Iinterface_2
{
public void Display()
{
Console.WriteLine("Class1 Display Method.");
}
public void interface1_method()
{
Console.WriteLine("Iinterface_1 Method Implicit interface implementation.");
}
public void interface2_method()
{
Console.WriteLine("Iinterface_2 Method Implicit interface implementation.");
}
void Iinterface_1.interface1_method()
{
Console.WriteLine("Iinterface_1 Method Explicit interface implementation.");
}
void Iinterface_2.interface2_method()
{
Console.WriteLine("Iinterface_2 Method Explicit interface implementation.");
}
}
class Program
{
static void Main(string[] args)
{
Class1 objClass1 = new Class1();
objClass1.interface1_method();
objClass1.interface2_method();
Iinterface_2 obj_2 = new Class1();
obj_2.interface1_method();
obj_2.interface2_method();
Console.ReadLine();
}
}
Output:
Iinterface_1 Method Implicit interface implementation.
Iinterface_2 Method Implicit interface implementation.
Iinterface_1 Method Explicit interface implementation.
Iinterface_2 Method Explicit interface implementation.
Note
1. Observe the following statements.
public void interface1_method() //Working
void Iinterface_1.interface1_method() //Working
public void Iinterface_1.interface1_method() //Error because of public.
Example 10
The following is an example of interface inheritance with the same method name.
interface Iinterface_1
{
void interface1_method();
}
interface Iinterface_2 : Iinterface_1
{
void interface1_method();
}
class Class1 : Iinterface_2
{
public void Display()
{
Console.WriteLine("Class1 Display Method.");
}
public void interface1_method()
{
Console.WriteLine("Iinterface_1 Method Implicit interface implementation.");
}
void Iinterface_1.interface1_method()
{
Console.WriteLine("Iinterface_2 Method Explicit interface implementation.");
}
void Iinterface_2.interface1_method()
{
Console.WriteLine("Iinterface_1 Method Explicit interface implementation.");
}
}
class Program
{
static void Main(string[] args)
{
Class1 objClass1 = new Class1();
objClass1.interface1_method();
Iinterface_1 obj_1 = new Class1();
obj_1.interface1_method();
Iinterface_2 obj_2 = new Class1();
obj_2.interface1_method();
Console.ReadLine();
}
}
Output:
Iinterface_1 Method Implicit interface implementation.
Iinterface_1 Method Explicit interface implementation.
Iinterface_2 Method Explicit interface implementation.