Introduction
In this article, I am going to explain one of the most important concepts i.e. Interface, key points about interface & real-world scenarios where it is preferable.
Interface
- An interface defines a contract. Any class or struct that implements that contract must provide an implementation of the members defined in the interface. We can create an interface using the Interface keyword.
- Interface members are, by default, public, and we can't explicitly specify any access modifiers.
- The interface will contain only method declaration but no implementation. The implementation will be provided by the derived class, which will be inheriting the interface.
- The interface will contain properties and methods, but it will not contain fields.
- We can't create an object of an interface, but an interface reference variable can point to the child/derive class object.
- Interface will used for Code reusability purposes so that the same code can be reused across multiple projects.
- A class can inherit multiple interfaces at the same time, so we willn't get any compile errors. Whereas a class can't inherit multiple classes at the same time.
- C# will support multiple interface inheritance as C# willn't support multiple class inheritance.
Syntax
interface interfaceName
{
}
Example
interface IMyInterface
{
void MyMethod();
int MyProperty { get; set; }
}
Implementation of Interface
- A class can implement an interface by using the operator followed by the interface name.
- The class must then provide an implementation for all of the methods and properties defined in the interface.
Example
using System;
interface IMyInterface
{
void MyMethod();
int MyProperty { get; set; }
}
public class MyClass : IMyInterface
{
public int MyProperty { get; set; }
public void MyMethod()
{
Console.WriteLine("My Method!..");
}
}
Multiple Interface Inheritance
Interfaces can also inherit from other interfaces using the operator along with the comma(,) operator.
Example 1
public interface IMyInterface1
{
// Define members or methods here, if needed
}
public interface IMyInterface2
{
// Define members or methods here, if needed
}
public interface IMyInterface3
{
// Define members or methods here, if needed
}
public class Test : IMyInterface1, IMyInterface2, IMyInterface3
{
// Class members and methods can be defined here
}
Explanation. In the above example, I have created 3 interfaces, i.e, IMyInterface1, IMyInterface2, and IMyInterface3 & Test is a class which is inheriting all the interfaces at the same time. you willn't get any compile error while trying this approach. So it is confirmed C# supports multiple interface inheritance.
Example 2
public interface IMyInterface1
{
void MyMethod1();
}
public interface IMyInterface2
{
void MyMethod2();
}
public interface IMyInterface3
{
void MyMethod3();
}
public interface IMyInterface4 : IMyInterface1, IMyInterface2, IMyInterface3
{
// Any additional members or methods can be defined here
}
Note. C# willn't support multiple-class inheritance.
Example 3
public class MyClass1
{
// Class members and methods can be defined here
}
public class MyClass2
{
// Class members and methods can be defined here
}
public class MyClass3 : MyClass1, MyClass2
{
// Class members and methods can be defined here
}
Explanation. In the above example, i have created 2 classes, i.e, MyClass1, MyClass2 & created one more class MyClass3 where I am trying to inherit both MyClass1,MyClass2 at the same but this is not allowed in c#, it is violating the principle. You will get compile error. Hence it is confirmed C# willn't support multiple class inheritance.
Real world scenario where the interface is a preferable
program.cs
using System;
public interface ITouchpad
{
void Touchpad();
}
public abstract class Mobile
{
public abstract void SMSFunctionality();
public abstract void PlayStore();
}
public class Samsung : Mobile, ITouchpad
{
public override void SMSFunctionality()
{
Console.WriteLine("SMS Functionality of Samsung Class");
}
public override void PlayStore()
{
Console.WriteLine("PlayStore Functionality of Samsung Class");
}
public void Touchpad()
{
Console.WriteLine("Touchpad Feature of Samsung Class");
}
}
public class Nokia : Mobile
{
public override void SMSFunctionality()
{
Console.WriteLine("SMS Functionality of Nokia Class");
}
public override void PlayStore()
{
Console.WriteLine("PlayStore Functionality of Nokia Class");
}
}
public class LG : Mobile
{
public override void SMSFunctionality()
{
Console.WriteLine("SMS Functionality of LG Class");
}
public override void PlayStore()
{
Console.WriteLine("PlayStore Functionality of LG Class");
}
}
Main Method()
using System;
class Program
{
static void Main(string[] args)
{
Samsung objsamsung = new Samsung();
objsamsung.SMSFunctionality();
objsamsung.PlayStore();
objsamsung.Touchpad();
Console.WriteLine("*************************************************");
Nokia objnokia = new Nokia();
objnokia.SMSFunctionality();
objnokia.PlayStore();
Console.WriteLine("*************************************************");
LG objlg = new LG();
objlg.SMSFunctionality();
objlg.PlayStore();
Console.ReadLine();
}
}
0utput
Explanation. I have taken the same real world example which i have used in my other article i.e, What is Abstract Class in C#?.
Here I have taken the real world example of Mobile phone. In a mobile phone, we will have different common functionalities which will be used in different brand i.e. Nokia and Samsung.
LG etc. As per the above code I have created an abstract class Mobile which will contain 2 abstract methods, i.e. SMSFuntionality & PlayStore. The 2 Child/derive classes.
i.e. Samsung & Nokia Inherited the abstract class & implemented the method.
Let's suppose after some time Samsung mobile brand wants a new feature i.e. Touchpad functionality so in that case if we will be a define a new abstract method
Example. TouchpadFunctionality in the abstract class then in every derive class it is mandatory to implement the new abstract method, but this is not the correct approach also
Nokia, LG brand didn't mention regarding the Touchpad feature. So this will not consider as a valid approach if we will define a new abstract method in the abstract class.
So to overcome this scenario what we can do is instead of defining a new abstract method in the abstract class we can create a new interface example ITouchpad where we will define
A method called Touchpad. Now Samsung class will inherit both abstract class (Mobile) & interface (ITouchpad), implement the 3 methods, i.e. SMSFuntionality, PlayStore & Touchpad.
Once we will implement this approach, we can achieve the expected output, i.e. Samsung derive class will consume all the 3 methods without violating the requirement comes from Samsung
Brand mobile.
So in the above scenario interface is most preferable.