Monday, May 6, 2013

Interfaces in C#


Introduction

An interface look like a class, but has no implementation, or say only one thing it contains are declaration of indexes, methods, events or properties, Interface in C# provide a way to achieve runtime ploymorphism ( means in which compiler identifies which polymorphism from to execute at run time but not at compile time is called as rum time polymorphism or late binding) . 
A simple example that shows how to look like an interface in c#

class Iexample
    {
        static void Main(string[] args)
        {
            System.Console.WriteLine("Hello");
        }
    }
    interface demo
    {
    }
Output
 

Hello
 


No problem, the above program will be run successfully, the above program consist a

class Iexample,

Main() is the entry point and an interface with the name as demo, at this time Interface
demo is empty, Now

I try to add some element in it.

What can we not do with interfaces, Lets see
1- Interface can not contain variable or variables
class Iexample
    {
        static void Main(string[] args)
        {
            System.Console.WriteLine("Hello");
        }
    }
    interface demo
    {
       int  x;
    }
When you compile this program it will  simply gives an error "Interface cannot contain

fields".
2- Interface member can not have definition.
  class Iexample
    {
        static void Main(string[] args)
        {
            System.Console.WriteLine("Hello");
            Console.ReadKey();
        }
       }
 
    interface demo
    {
        void abc()
        {
            System.Console.WriteLine("Interface member can not have definition");
        }
 
    }
When you compile this program it will  simply gives an error "Interface can not have

definition
".
3- when you create an interface and inherit that interface by the any class, then you must implement that interface in that class, otherwise it will gives a error.
class Iexample:demo
    {
        static void Main(string[] args)
        {
            System.Console.WriteLine("Hello");
            Console.ReadKey();
        }
       }
 
    interface demo
    {
        void abc();
       
    }
When you compile this program it will  simply gives an

error  "'consoleApplication1.Iexample' does not implement the interface member

consoleApplication1.demo.abc()
'".
4- when you create an interface and inherit that interface by the any class, then you must implement that interface in that class and that interface method must be implemented with access modifier "public" otherwise it will generate an error. Look a simple program.
class Iexample:demo
    {
        static void Main(string[] args)
        {
            System.Console.WriteLine("Hello");
            Console.ReadKey();
        }
        void abc()
        {
            Console.WriteLine("Implement with public");
        }
       }
 
    interface demo
    {
        void abc();
       
    }
When you compile this program it will  simply gives an error "'consoleApplication1.Iexample' does not

implement the interface member 'consoleApplication1.demo.abc()
'" and also

"'consoleApplication1.Iexample' cannot not implement the interface member because it is not public."


Now we move out with right example of interface

Example of Interface (With method)
 
using System;
namespace ConsoleApplication11
{
    class Iexample:demo
    {
        static void Main(string[] args)
        {
            demo obj = new Iexample(); //decalare an interface instance
            obj.abc();
            System.Console.WriteLine("Bye....");
            Console.ReadKey();
        }
       public void abc()
        {
            Console.WriteLine("Fun with Interface");
        }
       }
 
    interface demo
    {
        void abc();
       
    }
   
}
 
Output
 

Fun with Interface
Bye....
 

As we say above, Interface contains are declaration of indexes, methods, events or properties, Now have

p
lay with method, Now Its time to play with properties.
Example of Interface (With properties)
using System;
namespace ConsoleApplication11
{
    class Iexample:InterfaceImnpelment
    {
        static void Main(string[] args)
        {
            InterfaceImnpelment obj = new Iexample();
            obj.Prop1 = 1;
            obj.Prop2 = "dothearts";
            Console.WriteLine("Rank of " + obj.Prop2 + " is " + obj.Prop1);
            Console.ReadKey();
        }
     
       }
    class InterfaceImnpelment : demo
    {
        private int _prop1;
        private string _prop2;
        public int Prop1
        {
            get { return this._prop1; }
            set { this._prop1 = value; }
        }
        public string Prop2
        {
            get { return this._prop2; }
            set { this._prop2 = value; }
        }
    }
    interface demo
    {
        int Prop1
        {
            get;
            set;
        }
        string Prop2
        {
            get;
            set;
        }
    }
   
}
Output
 

Rank of dotnethearts is 1

You can also implement more than one interface into a single, which have inherits its.
using System;
namespace ConsoleApplication11
{
    class Iexample : MultiInterfaces
    {
        static void Main(string[] args)
        {
            MultiInterfaces obj = new Iexample();
            obj.fun1();
            obj.fun2();
            Console.ReadKey();
        }
    }
    // implement more than one interface in a single class
    class MultiInterfaces : demo1,demo2 
    {
        public void fun1()
        {
            Console.WriteLine("Hi!");
        }
        public void fun2()
        {
            Console.WriteLine("Bye!");
        }
    }
    interface demo1
    {
        void fun1();
    }
    interface demo2
    {
        void fun2();
    }
}

Advantage of interface
  • It provide the facility to reuse of software.
  • By the help of interface you can hide the implementation details of classes from each other.
  • Code reusability.