Saturday 2 May 2015

What is a Interface in C# .Net

An interface looks like a class, but has no implementation.The reason interfaces only provide declarations is because they are inherited by classes and structs, which must provide an implementation for each interface member declared.

Properties Of Interface:-

  1. Supports multiple inheritance(Single Class can implement multiple interfaces).
  2. Can not Contains data members.
  3. By Default interface members is public (We Can not set any access modifier into interface members).
  4.  Interface can not contain constructors.



Interface IMyInterface
{
    void MethodToImplement();
}

Interface is a type which contains only the signatures of methods, delegates or events, it has no implementation. Implementation of the methods is done by the class that which implements the interface.
Interface is a contract that defines the signature of the functionality. So if a class is implementing a interface it says to the outer world, that it provides specific behavior. Example if a class is implementing ‘Idisposable’ interface that means it has a functionality to release unmanaged resources.

If a class implements a interface then it has to provide implementation to all its methods.



Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Threading.Tasks;
Using System.IO;
Using System.Globalization;
Using System.Data;

Namespace ConsoleDemoApp
{
    Class Test
    {
        /* program to implement interface in c# */

        Public static void Main(string[] args)
        {
            double dueAmt, ServiceCharge, Receipt;

           
            Console.Write("\nEnter Due Amount : ");
            dueAmt = Convert.ToDouble(Console.ReadLine());
            Console.Write("\nEnter Service Charge: ");
            ServiceCharge = Convert.ToDouble(Console.ReadLine());

            Payment pay = new Payment();
            // make payment
            pay.PayDueAmt(dueAmt, ServiceCharge);
            //print payment history
            Receipt = pay.getTotalPaidAmt();
            Console.Write("\nTransaction is successful");
            Console.Write("\nTotal paid amount is : {0}", Receipt);
            Console.ReadKey();
        }
    }

    // Interface
    public interface IPayment
    {
        // member of interface
        Void PayDueAmt(Double dueAmt, Double serviceCharge);
        Double getTotalPaidAmt();
      
    }

    // class Payment implements the above interface
    Public class Payment : IPayment
    {
        Double paidAmt, ServiceCharge, Total;
        // method 1
        Public void PayDueAmt(Double dueAmt, Double serviceCharge)
        {
            this.paidAmt = dueAmt;
            this.ServiceCharge=serviceCharge;
        }
        // method 2
        Public Double getTotalPaidAmt()
        {
            this.Total = this.paidAmt + this.ServiceCharge;
            Return this.Total;
        }
    }
}


  Output














Summary
You now understand what interfaces are. You can implement an interface and use it in a class. Interfaces may also be inherited by otherinterface. Any class or struct that inherits an interface must also implement all members in the entire interface inheritance chain.

No comments:

Post a Comment