C# Interfaces

Interface provides a contract among programmers, using which the programmers agree upon the name of functions, their return types and input arguments. Only the signatures of functions and properties are defined within a interface. This is different from an abstract class, where a function can be defined within body of an abstract class.

In main function, we instantiate an interface object with the name of the class derived from the interface. Interfaces support inheritance. A class derived from a derived interface must implement all the method/property signatures defined in both the parent and child interfaces. The following is a complete example of interfaces, including interface inheritance.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;


namespace ConsoleApplication6
{
    /* the main interface defined for a bank account. It was two function signatures for depositing amount in account and for withdrawing amount from account. The third property shows the current balance */

    public interface IBankAccount
    {
        void PayIn(decimal amount);
        bool WithDraw(decimal amount);
        decimal Balance { get;}   
    }

/* The interface derived from the above main interface. This interface uses an extra function TransferTo that will allow transfer of amount in a target account (defined as interface) and the amount to be transferred */

    public interface ITransferBankAccount : IBankAccount
    {
        bool TransferTo(IBankAccount destination, decimal amount);
    }


    public class CurrentAccount : IBankAccount
    {
        private decimal balance;
        public void PayIn(decimal amount)
        {
            Console.WriteLine("Deposit amount in Current Account");
            balance += amount;
        }

        public bool WithDraw(decimal amount)
        {
            if (balance >= amount)
            {
                balance -= amount;
                Console.WriteLine("amount withdrawn");
                return true;
            }
            Console.WriteLine("Withdrawal attempt failed.");
            return false;
        }

        public decimal Balance
        {
            get
            {
                return balance;
            }
        }

        public override string ToString()
        {
            return String.Format("Current Account Balance = {0,6:C}", balance);

        }

    }

/* as the below class is derived from a derived interface, only this class is implementing the TransferTo function */
    public class SaverAccount : ITransferBankAccount
    {
        private decimal balance;

        public bool TransferTo(IBankAccount destination, decimal amount)
        {

            bool result;
            result = this.WithDraw(amount);

            if(result == true)
            {
                destination.PayIn(amount);
            }

            return result;
        }

        public void PayIn(decimal amount)
        {
            Console.WriteLine("Deposit amount in Saving Account");
            balance += amount;
        }

        public bool WithDraw(decimal amount)
        {
            if(balance>=amount)
            {
                balance -= amount;
                Console.WriteLine("amount withdrawn");
                return true;
            }
            Console.WriteLine("Withdrawal attempt failed.");
            return false;
        }

        public decimal Balance
        {
            get
            {
                return balance;
            }
        }

        public override string ToString()
        {
            return String.Format("Saving Balance = {0,6:C}", balance);

        }
    }
    
    class Program
    {
        static void Main(string[] args)
        {

  // an object of SaverAccount class is created and initalized in interface variable
            ITransferBankAccount saver = new SaverAccount();        

// 500 is deposited in the saver account
            saver.PayIn(500);

// an object of CurrentAccount class is created
            IBankAccount current = new CurrentAccount();

// an amount of 400 is transferred from saver account to current account
            saver.TransferTo(current, 400);

// the remaining amounts of both current and saver are shown in the end
            Console.WriteLine(current.ToString());
            Console.WriteLine(saver.ToString());
        }
    }
}

 

 

Add a Comment

Your email address will not be published. Required fields are marked *