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
Output: Class1 Display Method
Example 2
The following is an example of Implicit and Explicit interface implementation:
Output: Class1 Display Method.
Example 3
The following is an example of how to call an Implicit interface method:
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.
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.
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.
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.
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.
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.
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.
Example 10
The following is an example of interface inheritance with the same method name.
Output:
Iinterface_1 Method Implicit interface implementation.
Iinterface_1 Method Explicit interface implementation.
Iinterface_2 Method Explicit interface implementation.