17 - Interface in C#

17.1 Purpose of Interfaces

An interface in C# has its structure similar to a class but it does not contain any implementation. Interface defines properties, methods, and events that are known as members of it. Classes and structures also implement these interface members. They are used as a replacement to the use of multiple inheritance, as it is not supported in C#.

An interface is used when you want a standard structure of methods to be followed by the classes, where the classes implement the functionality. Interface defines what a class can do, but it does not define how the class does it.

The different purpose of using interfaces is as mentioned below:

1) It is used for the reuse of the software

2) It creates loosely coupled software

3) It allows different objects to interact easily with each other

4) It separates the definition of objects from their implementation

5) It provides user to achieve runtime polymorphism

6) Interface inheritance provides the concept of name hiding. It is an ability to hide an inherited member from the code outside the derived class

17.2 Define an Interface

Working with interfaces includes declaration and implementation of interfaces by the classes. User can declare only methods, functions and properties in interfaces. You cannot declare a variable in interface.

User can declare an interface by using the interface keyword. The declaration of an interface is similar to a class declaration. The statements in an interface are public. The following code snippet shows the declaration of an interface.

public interface IMyInterface
{
    //Declare an interface member
    void MethodToImplement();
}

The interface named IMyInterface is defined in the above snippet. According to the naming conventions, the prefix of the interface name should be with capital letter ‘I’. The interface declares a single method as MethodToImplement(). The method does not contain any implementation but it ends with a semicolon. The interface specifies only the signature of the methods that a inheriting class or a struct must implement.

public interface IGame
{
    void show();
}
class Game : IGame
{
    int _noofplayers;
    string _sportType;
    double _width;
    double _breadth;

    public Game ( int nop, string sptype, double wid, double bdth )
    {
        _noofplayers = nop;
        _sportType = sptype;
        _width = wid;
        _breadth = bdth;
    }
    public void show()
    {
        Console.WriteLine(“The description is: “);
        Console.WriteLine(“No of players are {0}”, _noofplayers);
        Console.WriteLine(“The type of sport is {0}”, _sportType);
        Console.WriteLine(“The width of the ground is {0}”, _width );
        Console.WriteLine(“The Breadth of the ground is {0}”, _breadth);
    }
}
class Output
{
    static void Main ( string[ ] args )
    {
        Game s = new Game (20, “BaseBall”, 20, 30);
        s.show();
        Console.Read();
    }
}

A code sample demonstrating the implementation of an interface is as shown below:

The output for the code is as shown below

:

In the above code, the interface named IGame is defined. It consists of variables as noofplayers, sport type, width and breadth of the ground. The variables are initialized in the constructor of the class. The method for displayed the output is defined.

17.3 Use an Interface

There are different uses of an interface in C# are as mentioned below:

1) Interface helps user to define the behavioral model, defining what an object can perform in an application

2) It reduces the coupling and allows the user to easily change the implementations without affecting the code

3) If every class needs different implementations, better is the use of the concept of interface

4) The IDisposable interface guarantees a user that it has an Dispose() method

5) Interfaces can implement another interface in same application

17.4 Implement the Interface Inheritance

A class inherits all the interfaces defined in the same application. The interface can be inherited from another interface defined by the user. These interfaces can be then inherited by the class defining the methods for the interface definitions.

The general syntax of implementing the inheritance interface is as shown below:

interface IMyInterface1
{
    Void ParentInterfaceMethod();
}
interface IMyInterface2: IMyInterface1
{
    void MethodToImplement();
}
class InterfaceImplementer:IMyInterface2
{
    //methods to be implemented
}

A code sample for implementing interface inheritance is as shown below:

interface IParent
{
    void Parentinterface();
}
interface IResult : IParent
{
    void InheritedImplement();
}
class Program : IResult
{
    public void InheritedImplement()
    {
        Console.WriteLine(“The method is implemented in the class”);
    }
    public void ParentInterface()
    {
        Console.WriteLine(“The Parent interface is used in the class”);
    }
    static void Main ( string[ ] args )
    {
        Program p = new Program();
        p.InheritedImplement();
        p.ParentInterface();
        Console.Read();
    }
}

The output for the code is as shown below:

A class or a structure that implements interfaces also implements the base interfaces of its inherited interfaces. In the above code, the IResult interface inherits the member function, ParentInterface(), from the base interface IParent. If the class implements the IResult interface, it implicitly implements the IParent interface.

Like us on Facebook