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

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 interface?
  2. Why to user interface?
  3. When to use interface?
  4. How to use interface?
  5. Advantage of interface?
All the above questions are pointing to one question, which is use of interface. Let’s see….
 

Use of interface in C#

  1. Achieve/Support the Multiple Inheritance
  2. Achieve loose coupling Code
  3. Provides abstraction behaviours
  4. Support extensible
  5. Interfaces add a plug and play like architecture into your applications.
  6. With interfaces, we can define a contract (agreement or blueprint) and provide to the client.
  7. Unit testing is possible. Create the mock or stub the class objects and pass it via interface
  8. Implement Dependency injection

Achieve/Support the Multiple Inheritance

  1. With interface, we can achieve multiple inheritance in C#.
  2. How?
  3. See the below example: - Here I have two base classes. One is SavingAccount and another is CurrentAccount I am extending both classes into Account class. The C# complier giving the error “Class 'Account' cannot have multiple base classes: 'SavingAccount' and 'CurrentAccount'”. This says that using base class, multiple inheritance is not supported in c#.

    Use Of Interface With Real Time Examples, Interview Questions And Answers About Interface In C#
  1. Then how to achieve this functionally using interface in C#?
  2. Here we go. Just modify the code as below to achieve the multiple inheritance using interfaces.
  3. Example: - Here I have replaced the SavingAccount class with ISavingAccount interface and CurrentAccount class with ICurrentAccount. Instead of extending the two classes into account class, I just implemented both the interfaces (i.e. ISavingAccount & ICurrentAccount ) into Account class.

    Use Of Interface With Real Time Examples, Interview Questions And Answers About Interface In C#
  1. Now, I am able to use both the methods from two interfaces into to the Account class.
  2. This will solve my multiple Inheritance issue in C#.

Achieve loose coupling Code

  1. With the help of interface, we can achieve loose coupling code. How?
  2. Just Google what is tightly coupled and loosely coupled code.
  3. Below is an example of tightly couple code.
  4. Example: Here, I have created two SavingAccounts and CurrentAccounts. Now I am using both the class objects into Account class.

    Use Of Interface With Real Time Examples, Interview Questions And Answers About Interface In C#
  1. Tomorrow, if any new pension account class comes, then I need to change the Account
  2. Example:

    Use Of Interface With Real Time Examples, Interview Questions And Answers About Interface In C#
  1. Now how to achieve loosely coupled code using interface?
  2. Create the new IAccount. Just implement this interface in the SavingAccount, CurrentAccount, and PensionAccount class as per below.

    Use Of Interface With Real Time Examples, Interview Questions And Answers About Interface In C#
  1. Modify the Account class code as per below. Here I am passing the IAccount interface which we created above and Account class only know only the IAccount So on runtime, you can create the object of different saving account class and pass as a parameter to this Account class constructor. Note, the class object that you are passing Account class must implement the IAccount interface.
  2. Example,

    Use Of Interface With Real Time Examples, Interview Questions And Answers About Interface In C#
Interface provides the abstraction behaviours or Implementation Hiding or Provide required method(s)/functionalities to the client,
  1. Interface hides the implementation of the code.
  2. How? Let see.
  3. Example - I have two classes. One is SavingAccount and Account I have created the SavingAccount class object into Account class object. This Account class know the details implementation of the SavingAccount details.

    Use Of Interface With Real Time Examples, Interview Questions And Answers About Interface In C#
  1. This approach will not 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.
  2. So how to overcome this problem? By using interface.
  3. Example,

    Use Of Interface With Real Time Examples, Interview Questions And Answers About Interface In C#
  1. Here, I have created the IAccount interface and provide this interface to my Account class.
  2. Account class doesn’t know which class it is using because it does not have class object access.
  3. On run time, we can decide which class object we need to pass to Account via interface.
  4. This way, we can achieve or provide only required method(s)/functionalities to client.

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

 
The question may be Can I add new method(s) or functionalities to an interface without changing my client code? The answer is YES. How? Let’s see…..
  1. Say I have already created below example functionalities and delayed on server.
  2. Example - 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 ).

    Use Of Interface With Real Time Examples, Interview Questions And Answers About Interface In C#
  1. Now after some days, there is 3rd user comes and want to use the saving and current accounts details both in one interface. So how to achieve this?
  2. One way to add new method (i.e. GetCurrentAccountDetails()) into existing IAccount interface, then my code will break.

    Use Of Interface With Real Time Examples, Interview Questions And Answers About Interface In C#
  1. But I don’t want to change my UserOne & UserTwo Is there way to do it (i.e. one interface with both saving and current account details)?
  2. Then How? Here we Gooooooo……
    Here I have created 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.
  1. Example,

    Use Of Interface With Real Time Examples, Interview Questions And Answers About Interface In C#
  1. Interview may ask question in different way.

Real time example of interface

 
Let’s say I have Account class which implement ISavingAccount and ICurrentAccount interface.
 
Example,
  1. public interface ISavingAccount  
  2. {  
  3.     void PrintAccountData();  
  4. }  
  5.   
  6. public interface ICurrentAccount  
  7. {  
  8.     void PrintAccountData();  
  9. }  
  10.   
  11. public class Account : ISavingAccount, ICurrentAccount  
  12. {  
  13.     void ISavingAccount.PrintAccountData()  
  14.     {  
  15.         Console.WriteLine("Saving account data.");  
  16.     }  
  17.   
  18.     void ICurrentAccount.PrintAccountData()  
  19.     {  
  20.         Console.WriteLine("Current account data.");  
  21.     }  
  22. }  
  23.   
  24. public class Program  
  25. {  
  26.     static void Main(string[] args)  
  27.     {  
  28.         ISavingAccount savingAccount = new Account();  
  29.         ICurrentAccount currentAccount = new Account();  
  30.         savingAccount.PrintAccountData();  
  31.         currentAccount.PrintAccountData();  
  32.         Console.ReadLine();  
  33.     }  
  34. }  
Output
 
Use Of Interface With Real Time Examples, Interview Questions And Answers About Interface In C#
 
Happy Coding………
https://www.codementor.io/vipinc/when-to-use-abstract-classes-and-when-interfaces-c-interview-questions-jtzll1sn7
https://stackoverflow.com/questions/10914802/why-i-should-go-for-interfaces-in-c-sharp-when-i-can-implement-the-methods-direc