Object Oriented Programming

What Is An Interface & How To Use It ?

In this article, you’ll learn what is an interface, how to use it and the advantages of using an interface in Asp.Net C#.

Assume a real world scenario where you need to create an employee management system. In an employee management system, you will be having different type of employees like engineers, managers, sub-engineers etc. So, what you’ll do is create an interface called IEmployee which will applicable for all types of employee classes.

What is an Interface?

An interface is  contract or set of rules to be followed by the inheriting class. Let’s create our interface IEmployee:


interface IEmployee
{
    string GetFullName();
    string GetAddress();
}

Create a class for employee Engineer by inheriting the interface IEmployee.


class Engineer : IEmployee
{
    private string _firstName { get; set; }
    private string _lastName { get; set; }
    private string _address { get; set; }

    public Engineer(string firstName, string lastName, string address)
    {
        this._firstName = firstName;
        this._lastName = lastName;
        this._address = address;
    }

    public string GetFullName()
    {
        return this._firstName + this._lastName;
    }
    public string GetAddress()
    {
        return this._address;
    }
}

The class inheriting the interface should implement the methods defined in the interface. If you remove the implementation from the class Engineer and try to build the code, it would throw an error.
Similarly, create a class for employee Manager by inheriting from the interface IEmployee.


class Manager : IEmployee
{
    private string _firstName { get; set; }
    private string _lastName { get; set; }
    private string _address { get; set; }

    public Manager(string firstName, string lastName,string address)
    {
        this._firstName = firstName;
        this._lastName = lastName;
        this._address = address;
    }

    public string GetFullName()
    {
        return this._firstName+ this._lastName;
    }
    public string GetAddress()
    {
        return this._address;
    }
}

Create a method called PrintName which would print the name of the Manager object.

 
public static void PrintName(Manager obj)
{
    Console.WriteLine(obj.GetFullName());
}

Inside the main program create an object of the Manager object and call the PrintName method.

 
static void Main(string[] args)
{
    Manager manager = new Manager("Roy", "Agasthya", "New York");
    PrintName(manager);
}

If you try running the program it should print the full name of the manager.

But how would you make it generic so that it supports all types of employees ?

Instead of passing the Manager object type, let’s pass in the interface IEmployee as reference.

 
public static void PrintName(IEmployee obj)
{
    Console.WriteLine(obj.GetFullName());
}

Advantages Of Using An Interface In C# ?

The advantage of using interface is that the PrintName method would support all classes derived from interface as parameter. In future if a new class called TechincalManager is created derived from the interface IEmployee, the method PrintName would still be working fine without any modifications.

 
static void Main(string[] args)
{
    Manager manager = new Manager("Roy", "Agasthya", "New York");
    PrintName(manager);

    Engineer engineer = new Engineer("Sam", "Jose", "New York");
    PrintName(engineer);
}

You should not be using an Interface for designing your application if you tend to modify the interface in future. Interface once designed should not broken or modified since it would require all the derived classes to make the modification. For example, in the above example if you add a new method prototype called GetFamilyIncome to the interface IEmployee, the change should be made across all the derived classes which is not practical in scenarios where 100’s of classes have derived from the interface. In such scenarios you have Abstract classes in C#.

Conclusion

In this tutorial, you saw what is an interface, how to use it and the advantages of using an interface. Do let us know your thoughts, suggestions or any corrections in the comments below.