Interface In C#

Introduction

In this article, we will discuss interfaces and how to easily implement inheritance in them. First, we will look at what an interface is.

  • An interface is a way to define a set of methods that we implement by a derived class or multiple derived classes.
  • Two classes can implement the same interface in a different way.
  • It is basically a logical structure that describes the functionality (methods) but not its implementation( body).
  • A class must provide the bodies of the methods described in an interface to implement the methods.

We can declare an interface in the following way.

interface IName
{
    ReturnType MethodName1(ParameterList);
    ReturnType MethodName2(ParameterList);
}

We declare an interface with the keyword interface. Here "name" is the name of the interface. In an interface, all the methods are public, so there is no requirement for explicit access specifiers.

Example

public interface ICalc
{
    int Add(int a, int b);
    int Sub(int x, int y);
}

Here ICalc is the name of the interface, and add and sub are the methods of the interface.

Now we implement an interface in the following way.

class ClassName : InterfaceName
{
    // Body of the class
}

A class can implement more than one interface. We can declare each interface in a comma-separated list. In this case, the name of the base class is always first.

Example

class Calc: ICalc
{
public int add (int a, int b)
{
}
return a+b;
public int sub (int x, int y)
{
}
}
return x-y;

Output

The Addition is:5

The Subtraction is:1

Complete Program

using System;
public interface ICalc
{
    int Add(int a, int b);
    int Sub(int x, int y);
}
class Calc : ICalc
{
    public int Add(int a, int b)
    {
        return a + b;
    }

    public int Sub(int x, int y)
    {
        return x - y;
    }
}
class MainCalc
{
    public static void Main(string[] args)
    {
        Calc c = new Calc();
        Console.WriteLine("The Addition Is: " + c.Add(2, 3));
        Console.WriteLine("The Subtraction Is: " + c.Sub(3, 2));
    }
}

Inheritance in the Interfaces

Interfaces can be inherited. Now we look at the example of how to easily inherit an interface.

public interface IFirst
{
    void PrintA();
    void PrintB();
}
public interface ISecond : IFirst
{
    void PrintC();
}

After inheritance, "second" has the methods of the first interface, and it adds printC() to it. Now the class MyClass can access all three methods.

class MyClass : ISecond
{
    public void PrintA()
    {
        Console.WriteLine("Print A");
    }
    public void PrintB()
    {
        Console.WriteLine("Print B");
    }
    public void PrintC()
    {
        Console.WriteLine("Print C");
    }
}

Complete Program

using System;
public interface IFirst
{
    void PrintA();
    void PrintB();
}
public interface ISecond : IFirst
{
    void PrintC();
}
class MyClass : ISecond
{
    public void PrintA()
    {
        Console.WriteLine("Print A");
    }
    public void PrintB()
    {
        Console.WriteLine("Print B");
    }
    public void PrintC()
    {
        Console.WriteLine("Print C");
    }
}
class MyMainClass
{
    public static void Main()
    {
        MyClass a = new MyClass();
        a.PrintA();
        a.PrintB();
        a.PrintC();
    }
}


Recommended Free Ebook
Similar Articles