Home > DeveloperSection > Blogs > Interface in c#

Interface in c#


C# C# 
Ratings:
0 Comment(s)
 1672  View(s)
Rate this:

Interface in c#

In this blog I am trying to explore the concept of Interface in c#.

Interfece:

An interface looks like a class, but has no implementation. The only thing it contains are declarations of events, indexers, methods and/or properties. The reason interfaces only provide declarations is because they are inherited by classes and struts, which must provide an implementation for each interface member declared.

An interface is defined as a signed contract that all the classes inheriting the interface should follow. The interface defines the 'what' part of contract and the deriving classes define the 'how' part of that contract must be accomplished.

Interfaces define properties, methods and events, which are the members of the interface. Interfaces contain only the declaration of the members. It is the responsibility of the deriving class to define the members. It often helps in providing a standard structure that the deriving classes would follow.

In logical term interface is fully abstract, because no any methods are concrete inside the interface. As in case of abstract class there may be some concrete methods along with abstract methods.

Abstract classes to some extent serve the same purpose, however, they are mostly used when only few methods are to be declared by the base class and the deriving class implements the functionalities.

 How to declare interface:

Interfaces are declared using the interface keyword. It is similar to class declaration. Interface statements are public by default. Following is an example of an interface declaration: in c# by convention all interface name starts with “I” as keyword as IComparable

public interface ITransactions

   {

      // interface members

      void showTransaction();

      double getAmount();

   }

Let’s take an example to illustrate the interface

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Data.SqlClient;

using System.Collections.Generic;

namespace interfaceExample

{

    class Program

    {

        static void Main(string[] args)

        {

            // instance of mytransaction

            MyTransaction t1 = new MyTransaction("110", "01/05/2012", 1000.00);

            //Another instance of myTransaction

            MyTransaction t2 = new MyTransaction("102", "08/12/2010",15000.00);

            //call show method of mytransactin class,

            //which is defined by mytransaction og Itransaction methods

            t1.showTransaction();

            t2.showTransaction();//same as above

            Console.ReadKey();

        }

    }//close class

   public interface ITransactions

   {

      // interface members

       // method of interface which deriving class must define

      void showTransaction();

      // another  method of interface which deriving class must define

      double getAmount();

   }//close interface

   public class MyTransaction : ITransactions

   {

      private string tCode;

      private string date;

      private double amount;

      public MyTransaction()// constructor

      {

         tCode = " ";

         date = " ";

         amount = 0.0;

      }

      public MyTransaction(string c, string d, double a)//constructor with parameter

      {

         tCode = c;

         date = d;

         amount = a;

      }

      // interface defined method is defined by derived class here.

      public double getAmount()

      {

         return amount;

      }

      // interface defined method is defined by derived class here.

      public void showTransaction()

      {

         Console.WriteLine("Transaction: {0}", tCode);

         Console.WriteLine("Date: {0}", date);

         Console.WriteLine("Amount: {0}", getAmount());

      }

   }//close class

}//close namespace


Don't want to miss updates? Please click the below button!

Follow MindStick