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.
- namespace InterfaceDemo
- {
- interface ITransaction
- {
- void doTransaction();
- double getAmount { get; }
- }
- }
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.
- namespace InterfaceDemo
- {
- class Transaction :ITransaction
- {
- public void doTransaction()
- {
- throw new System.NotImplementedException();
- }
-
- public double getAmount { get; private set; }
- }
- }
The following is the modified code for class implementation.
- using System;
-
- namespace InterfaceDemo
- {
- class Transaction : ITransaction
- {
-
- public void startTransaction()
- {
- Console.WriteLine("Started doing Transaction");
- }
- public void doTransaction()
- {
- Console.WriteLine("Doing Cash Transaction");
- }
-
- public double getAmount
- {
-
- get { return 10000; }
- }
- }
- }
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.
The following is the main class in its finished form.
- using System;
-
- namespace InterfaceDemo
- {
- class Program
- {
- static void Main(string[] args)
- {
- Transaction transaction = new Transaction();
-
- transaction.startTransaction();
- transaction.doTransaction();
- Console.WriteLine(transaction.getAmount);
- Console.ReadLine();
-
- }
- }
- }
Now, when I run the same, it will produce the following 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.
- using System;
-
- namespace InterfaceDemo
- {
- class Program
- {
- static void Main(string[] args)
- {
- ITransaction transaction = new Transaction();
- transaction.doTransaction();
- Console.WriteLine(transaction.getAmount);
-
-
-
-
-
- Console.ReadLine();
-
- }
- }
- }
With the above change in place, it will print the following output.
Also, we can have main method like shown below.
- using System;
-
- namespace InterfaceDemo
- {
- class Program
- {
- static void Main(string[] args)
- {
- var transaction = new Transaction();
- transaction.startTransaction();
- transaction.doTransaction();
- Console.WriteLine(transaction.getAmount);
-
-
- ITransaction iTransaction = transaction;
-
-
- Transaction transactionObj = iTransaction as Transaction;
-
-
- if(transactionObj!=null)
- transactionObj.startTransaction();
-
- Console.ReadLine();
-
- }
- }
- }
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.
Download
link.
With this, I would like to wrap this session here. Thanks for joining me.