Introduction
This article will give an answer to the below questions that most developers face in every interview. So what are those questions?
- What is the use of the Interface?
- Why do the user interface?
- When to use the Interface?
- How to use the Interface?
- What advantage of the Interface?
All the above questions are pointing to one question, which is the use of Interface. Let's see.
What is the use of Interface in C#?
- Achieve/Support the Multiple Inheritance
- Achieve loose coupling Code
- Provides abstraction behaviors
- Support extensible
- Interfaces add a plug-and-play-like architecture to your applications.
- With interfaces, we can define a contract (agreement or blueprint) and provide it to the client.
- Unit testing is possible. Create the mock or stub the class objects and pass it via the Interface.
- Implement Dependency injection.
Achieve/Support the Multiple Inheritance
With the Interface, we can achieve multiple inheritances in C#.
See the below example, here, I have two base classes. One is SavingAccount, and another is CurrentAccount I am extending both classes into the Account class. The C# complier gives the error "Class 'Account' cannot have multiple base classes: 'SavingAccount' and 'CurrentAccount'". This says that using base class, multiple inheritances are not supported in C#.
namespace ConsoleApp15
{
public class SavingAccount
{
public void PrintData()
{
Console.WriteLine("Saving account data.");
}
}
public class CurrentAccount
{
public void PrintData()
{
Console.WriteLine("Current account data.");
}
}
public class Account : SavingAccount, CurrentAccount
{
// Class 'Account' cannot have multiple base classes: "SavingAccount' and 'CurrentAccount'
}
}
How to achieve this functionally using Interface in C#?
Here we go. Just modify the code as below to achieve multiple inheritances using interfaces.Example, I have replaced the SavingAccount class with the ISavingAccount interface and CurrentAccount class with ICurrentAccount. Instead of extending the two classes into the account class, I just implemented both the interfaces (i.e. ISavingAccount & ICurrentAccount ) into the Account class.
You can use both methods from two interfaces in the Account class. This will solve my multiple Inheritance issue in C#.
Achieve loose coupling Code
With the help of the Interface, we can achieve loose coupling code, just Google what is tightly coupled and loosely coupled code.Below is an example of tightly coupled code, I have created two SavingAccounts and CurrentAccounts. Now I am using both the class objects in the Account class.
using System;
public class SavingAccount
{
public void PrintData()
{
Console.WriteLine("Saving account data.");
}
}
public class CurrentAccount
{
public void PrintData()
{
Console.WriteLine("Current account data.");
}
}
public class Account
{
SavingAccount savingAccount = new SavingAccount();
CurrentAccount currentAccount = new CurrentAccount();
public void PrintAccounts()
{
savingAccount.PrintData();
currentAccount.PrintData();
}
}
public class Program
{
static void Main(string[] args)
{
Account account = new Account();
account.PrintAccounts();
Console.ReadLine();
}
}
Tomorrow, if any new pension account class comes, then I need to change the Account
using System;
public class SavingAccount
{
public void PrintData()
{
Console.WriteLine("Saving account data.");
}
}
public class CurrentAccount
{
public void PrintData()
{
Console.WriteLine("Current account data.");
}
}
public class Account
{
SavingAccount savingAccount = new SavingAccount();
CurrentAccount currentAccount = new CurrentAccount();
public void PrintAccounts()
{
savingAccount.PrintData();
currentAccount.PrintData();
}
}
public class Program
{
static void Main(string[] args)
{
Account account = new Account();
account.PrintAccounts();
Console.ReadLine();
}
}
How to achieve loosely coupled code using the Interface?
Step 1. Create the new IAccount. Just implement this Interface in the SavingAccount, CurrentAccount, and PensionAccount classes as per below.
using System;
public class SavingAccount
{
public void PrintData()
{
Console.WriteLine("Saving account data.");
}
}
public class CurrentAccount
{
public void PrintData()
{
Console.WriteLine("Current account data.");
}
}
public class Account
{
SavingAccount savingAccount = new SavingAccount();
CurrentAccount currentAccount = new CurrentAccount();
public void PrintAccounts()
{
savingAccount.PrintData();
currentAccount.PrintData();
}
}
public class Program
{
static void Main(string[] args)
{
Account account = new Account();
account.PrintAccounts();
Console.ReadLine();
}
}
Step 2. Modify the Account class code as per below. Here I am passing the IAccount interface, which we created above, and the Accountknowsss only know only the IAccount So, on runtime, you can create the object of different saving account classes and pass it as a parameter to this Account class constructor.
using System;
public class SavingAccount
{
public void PrintData()
{
Console.WriteLine("Saving account data.");
}
}
public class CurrentAccount
{
public void PrintData()
{
Console.WriteLine("Current account data.");
}
}
public class Account
{
SavingAccount savingAccount = new SavingAccount();
CurrentAccount currentAccount = new CurrentAccount();
public void PrintAccounts()
{
savingAccount.PrintData();
currentAccount.PrintData();
}
}
public class Program
{
static void Main(string[] args)
{
Account account = new Account();
account.PrintAccounts();
Console.ReadLine();
}
}


Raditz VPosted Feb 15, 2022, 4:08 PM
Mahesh my man, 11/13 code examples are the same image.
Manali PatilPosted Aug 14, 2021, 6:36 AM
Kindly upload new pictures based on code, you have used the same pic everywhere
Munish kumarPosted May 23, 2021, 7:28 PM
Kindly upload new pictures based on code, you have used the same pic everywhere
Thirumalarao ThotapalliPosted May 30, 2020, 8:12 AM
Not mention program example every time you use one program
farukh shaikhPosted Mar 26, 2020, 2:54 AM
Sir where you create u create IAccount Interface and Last Extensible and understand partially not completally if u give programme then i understand 100 percent?