Use Of Interface With Real Time Examples, Interview Questions And Answers About Interface In C#

Introduction

This article will give an answer to the below questions that most developers face in every interview. So what are those questions?

  1. What is the use of the Interface?
  2. Why do the user interface?
  3. When to use the Interface?
  4. How to use the Interface?
  5. 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#?

  1. Achieve/Support the Multiple Inheritance
  2. Achieve loose coupling Code
  3. Provides abstraction behaviors
  4. Support extensible
  5. Interfaces add a plug-and-play-like architecture to your applications.
  6. With interfaces, we can define a contract (agreement or blueprint) and provide it to the client.
  7. Unit testing is possible. Create the mock or stub the class objects and pass it via the Interface.
  8. 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();
    }
}

Note. The class object you are passing the Account class must implement the IAccount interface.

The Interface provides the abstraction behaviors or Implementation Hiding or Provides required method(s)/functionalities to the client,

How Interface hides the implementation of the code?

I have two classes. One is SavingAccount, and Account I have created the SavingAccount class object into the Account class object. This Account class knows the details implementation of the SavingAccount details.

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();
    }
}

This approach will not be good if you are creating the web api or web services because the client will use your class object, and he will know all the methods present in that class, So how to overcome this problem? By using Interface.

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();
    }
}

I have created the IAccount interface and provided this Interface to my Account class, Account class doesn't know which class it is using because it does not have class object access.

On run time, we can decide which class object we need to pass to Account via Interface. This way, we can achieve or provide only the required method(s)/functionalities to the client.

Support extensible OR How to add new functionalities without changing the existing code?

The question may be Can I add a new method(s) or functionalities to an interface without changing my client code? The answer is YES. How? Let's see…

Say I have already created the below example functionalities and delayed on the server. Here IAccount interface has GetSavingAccountDetails() method that will provide only saving account details, and it is consumed by two user classes (i.e. UserOne & UserTwo ).

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();
    }
}

Now after some days, there is 3rd user comes and want to use the saving and current account details both in one Interface. So how to achieve this

One way to add a new method (i.e. GetCurrentAccountDetails()) into the existing IAccount interface, then my code will break.

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();
    }
}

But I don't want to change my UserOne & UserTwo Is there a way to do it (i.e. one Interface with both saving and current account details)

I have created a new ISavingAndCurrentAccount interface and inherited the existing IAccount interface. Now my UserThree can use this new Interface for both the saving and current account details without changing the other user class's code.

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();
    }
}

Real-time example of Interface

Let's say I have an Account class that implements ISavingAccount and ICurrentAccount interface.

Example

public interface ISavingAccount  
{  
    void PrintAccountData();  
}  
  
public interface ICurrentAccount  
{  
    void PrintAccountData();  
}  
  
public class Account : ISavingAccount, ICurrentAccount  
{  
    void ISavingAccount.PrintAccountData()  
    {  
        Console.WriteLine("Saving account data.");  
    }  
  
    void ICurrentAccount.PrintAccountData()  
    {  
        Console.WriteLine("Current account data.");  
    }  
}  
  
public class Program  
{  
    static void Main(string[] args)  
    {  
        ISavingAccount savingAccount = new Account();  
        ICurrentAccount currentAccount = new Account();  
        savingAccount.PrintAccountData();  
        currentAccount.PrintAccountData();  
        Console.ReadLine();  
    }  
}  

Output

 

Happy Coding………


Recommended Free Ebook
Similar Articles