Interface and Use of Interface

  • We create Interfaces using Interface keyword.
  • Just like a class, Interface contains methods, Properties, events, delegates but we can only define these terms in Interface and we don’t have implementation for that in an Interface.
  • Interface can’t contains Fields, constants, operators, constructors, destructors.
  • Interface members are public by default. We can’t explicitly define access specifiers on Interface members, if we do so, we will get compile time error. Also members should not be static.
  • Another class or struct who is using this interface, must provide implementation for all members of that Interface.
  • As we know that C# doesn’t support multiple class inheritance, We can achieve multiple inheritance using Interface.

Below is the Example for Interface

+
using System;
 
namespace Interfaces
{
    interface IMyInterface
    {
        void showMessage();
    }
 
    class Program : IMyInterface
    {
        public void showMessage()
        {
            Console.WriteLine("Interface method implemented");
        }
 
        static void Main(string[] args)
        {
            Program myProg = new Program();
            myProg.showMessage();
 
            Console.ReadLine();
        }
 
 
    }
}


In above code sample, We have declared interface IMyInterface and implemented it in Program class.

Now we know that C# doesn’t support multiple class inheritance. What is that mean? lets look at an example below.

+
using System;
 
namespace Interfaces
{
    class classA
    {
        public void Add()
        {
            Console.WriteLine("ClassA Add() method called");
        }
    }
 
    class classB
    {
        public void Add()
        {
            Console.WriteLine("ClassB Add() method called");
        }
    }
 
    class classC : classA, classB
    {
        public void Subtract()
        {
            Console.WriteLine("ClassC Subtract() called");
        }
    }
 
    class Program
    {
        static void Main(string[] args)
        {
            classC clsC = new classC();
            clsC.Add();
            Console.ReadLine();
        } 
    }
}

In Above code, We have created 2 class classA and classB both having same method declared Add(). Now suppose, for the time being, C# supports multiple inheritance, We have derived class classC which inherits both classA and classB, that means by using classC object we can access Add() method.
We have created Object of classC in our main method in Program class and we called Add() method using that object. Now in this case which Add() method gets called? There will be ambiguity in calling this method. This is the reason, C# doesn't allow multiple inheritance. If we compile above code, we will get compile time error saying Class 'Interfaces.classC' cannot have multiple base classes: 'Interfaces.classA' and 'classB'

Now see below code sample, where we achieved multiple inheritance using interface.

+
using System;
 
namespace Interfaces
{
    interface IClassA
    {
        void methodClassA();
    }
 
    class classA:IClassA
    {
 
        public void methodClassA()
        {
            Console.WriteLine("classA method gets called");
        }
    }
 
    interface IClassB
    {
        void methodClassB();
    }
 
    class classB : IClassB
    {
 
        public void methodClassB()
        {
            Console.WriteLine("classB method gets called");
        }
    }
 
    class classC:IClassA,IClassB
    {
        public void methodClassA()
        {
            new classA().methodClassA();
        }
 
        public void methodClassB()
        {
            new classB().methodClassB();
        }
    }
 
    class Program
    {
        static void Main(string[] args)
        {
            classC clsC = new classC();
            clsC.methodClassA();
            clsC.methodClassB();
 
            Console.ReadLine();
        }
    }
 }

In above code sample, classA and ClassB methods implements IClassA and IClassB respectively. In classC we have implemented interface IClassA and IClassB and called classA and classB's method directly by using objects of classA and classB.

Consider third case now, where we have two interface that have same method declared in it. Lets take an example below where we have two interfaces declared with same method name add()

+
using System;
 
namespace Interfaces
{
    interface InterfaceA
    {
        void add();
    }
 
    interface InterfaceB
    {
        void add();
    }
 
    class clsA:InterfaceA
    {
 
        public void add()
        {
            Console.WriteLine("clsA add() method gets called");
        }
    }
 
    class clsB : InterfaceB
    {
 
        public void add()
        {
            Console.WriteLine("clsB add() method gets called");
        }
    }
 
    class classAB:InterfaceA,InterfaceB
    {
 
        public void add()
        {
            new clsA().add();
        }
 
        void InterfaceB.add()
        {
            new clsB().add();
        }
    }
 
    class Program
    {
        static void Main(string[] args)
        {
            //sample 1
            InterfaceA clsA = new classAB();
            clsA.add();
 
            //sample 3
            InterfaceB clsB = new classAB();
            clsB.add();
 
            //sample 3
            classAB clsAB = new classAB();
            clsAB.add();
        }
 
 
    }
 
}

In the above case, When we implement both interface in classAB, We have to explicitly implement one of interface's add() method, just like in above code, we have implemented InterfaceB's add() method by prefixing interface name to it.

In the above code, if we want to call method of a respective class then create reference variable of that particular class's interface which will points to classAB object. See sample 1 in above code in Main method, where we want to call clsA's method. So we have created reference variable of interfaceA as class clsA implements InterfaceA.

Suppose, we Directly created reference variable of class clsAB and call the method add(), then by default first method that we written without interface name prefixed will get called i.e clsA's method. you can copy above code and check the final output.

If you see dot net framework, you can find all interfaces defined starting with letter 'I', e.g. IEnumerable, IEquatable, IComparable, IDisposable etc.

No comments :