Overview Of Interfaces

In today's section, we'll talk about Interfaces. Interfaces are constraints implementing class must follow. In one way, it is similar to abstract class but doesn't act as base class. C# doesn't have multiple inheritance. You can inherit from only one class but you can implement as many interfaces as you like. Also, one point to understand that access modifiers like private, public, protected, etc are not legal in case of interfaces. Hence, without wasting time, let's jump at the demo.

The following is the simple interface in its plain form.

  1. namespace InterfaceDemo    
  2. {    
  3.     interface ITransaction    
  4.     {    
  5.         void doTransaction();    
  6.         double getAmount { get; }    
  7.     }    
  8. }   
Now, let us go ahead and create one class which implements the same. The following is the default implementation for the same. You may also notice that since I am not having setter in interface, hence in implementation that came as private setter which means value can be set from constructor.
  1. namespace InterfaceDemo    
  2. {    
  3.     class Transaction :ITransaction    
  4.     {    
  5.         public void doTransaction()    
  6.         {    
  7.             throw new System.NotImplementedException();    
  8.         }    
  9.     
  10.         public double getAmount { getprivate set; }    
  11.     }    
  12. }   
The following is the modified code for class implementation.
  1. using System;    
  2.     
  3. namespace InterfaceDemo    
  4. {    
  5.     class Transaction : ITransaction    
  6.     {    
  7.         //Not part of interface    
  8.         public void startTransaction()    
  9.         {    
  10.             Console.WriteLine("Started doing Transaction");    
  11.         }    
  12.         public void doTransaction()    
  13.         {    
  14.             Console.WriteLine("Doing Cash Transaction");    
  15.         }    
  16.     
  17.         public double getAmount    
  18.         {    
  19.     
  20.             get { return 10000; }    
  21.         }    
  22.     }    
  23. }   
Now, let us go ahead and use the same in main class. While writing the class, you can see available properties and methods to get exposed.

transaction
The following is the main class in its finished form.
  1. using System;    
  2.     
  3. namespace InterfaceDemo    
  4. {    
  5.     class Program    
  6.     {    
  7.         static void Main(string[] args)    
  8.         {    
  9.             Transaction transaction = new Transaction();    
  10.     
  11.             transaction.startTransaction();    
  12.             transaction.doTransaction();    
  13.             Console.WriteLine(transaction.getAmount);    
  14.             Console.ReadLine();    
  15.     
  16.         }    
  17.     }    
  18. }   
Now, when I run the same, it will produce the following output.

output

However, It is also perfectly legal to have a variable of type Interface and instantiate a class like shown below. But, while doing so, here I won't be having method access which is declared and implemented explicitly in class.

Getamount
  1. using System;    
  2.     
  3. namespace InterfaceDemo    
  4. {    
  5.     class Program    
  6.     {    
  7.         static void Main(string[] args)    
  8.         {    
  9.             ITransaction transaction = new Transaction();    
  10.             transaction.doTransaction();    
  11.             Console.WriteLine(transaction.getAmount);    
  12.             //Transaction transaction = new Transaction();    
  13.     
  14.             //transaction.startTransaction();    
  15.             //transaction.doTransaction();    
  16.             //Console.WriteLine(transaction.getAmount);    
  17.             Console.ReadLine();    
  18.     
  19.         }    
  20.     }    
  21. }    
With the above change in place, it will print the following output.

Run

Also, we can have main method like shown below.
  1. using System;    
  2.     
  3. namespace InterfaceDemo    
  4. {    
  5.     class Program    
  6.     {    
  7.         static void Main(string[] args)    
  8.         {    
  9.             var transaction = new Transaction();    
  10.             transaction.startTransaction();    
  11.             transaction.doTransaction();    
  12.             Console.WriteLine(transaction.getAmount);    
  13.     
  14.             //Assigned the object to Interface variable    
  15.             ITransaction iTransaction = transaction;    
  16.             //Here also only interface stuffs will be available    
  17.             //However, if at all we would like to transaction explicit method available, then we need to cast like shown below    
  18.             Transaction transactionObj = iTransaction as Transaction;    
  19.             //Here, as keyword will examine the iTransaction and if it is of type Transaction, then it will return Transaction    
  20.             //else will return null. Hence, null check is recommended here    
  21.             if(transactionObj!=null)    
  22.                 transactionObj.startTransaction();    
  23.     
  24.             Console.ReadLine();    
  25.     
  26.         }    
  27.     }    
  28. }  
Here, I have used cast operator <strong>as</strong> to check whether the object is of type class or interface. And, this will produce the following output.

Code

Download link.

With this, I would like to wrap this session here. Thanks for joining me.

 


Similar Articles