Interface

Introduction

In object-oriented programming, An interface is a collection of abstract methods (without body implementations), properties, indexers, and events. It is used to define the behavior that can be implemented by the classes. It enforces classes to implement all the members of it.

For example, suppose we have a Truck class and a Car class. Each of these classes should have an action start engine, stop engine, break, etc. Actual implementation like How the engine is started for each vehicle depends on a particular class, but the fact that they must have a start and stop the engine, break action is the domain of it.

interface in c#, multiple interface in c#, interface in c# with real time example, c# interface tutorial, interface in oops with example c#, interface in java, interface design oop, implementing interface in C#

Why we need Interface?

Interfaces are more flexible, we can implement multiple interfaces to a class. As the class can inherit multiple interfaces, multiple languages (C#, Java) could be able to achieve Multiple Inheritance.
It also allows us to achieve full abstraction, without Interfaces we cannot achieve full abstraction.

In other words, we also say that it is a contract between the class that specifies class behavior.

It is used to define an abstract type that contains no data but defines behaviors as method signatures. A class having code and data for all the methods corresponding to that interface and declaring so is said to implement that interface. Furthermore, even in single-inheritance-languages, one can implement multiple interfaces, and hence can be of different types at the same time. you can read more

Wiki

Here are few bullet points about

  • It is the blueprint of the class which specify what a class must do and not how to do it.
  • It is about to define a common behavior as method signatures so other classes could implement a set of methods.
  • It expresses how we can interact with a class
  • Using it, we can implement multiple interfaces
  • It used to accomplish full abstraction and to achieve loose coupling

Syntax for defining it is as follows:

modifiers>] interface <Interface-Name>
{

//Abstract member declarations

}

Rules of Interface

Below are some rules which need to follow while working with it

  • It can’t be instantiated i.e. we cannot create an instance of it.
  • The default scope of the members is public.
  • We cannot have any member variables or fields. But we can define properties in it.
  • We cannot use access specifiers or other keywords like ‘abstract’ as default abstract and public.
  • It can inherit from another interface or from multiple interfaces.
  • A class can implement multiple interfaces.
  • A Class that implements an interface must implement all the members of it.

Implementing an Interface

There are two ways of implementing the members

  • implicit implementation
  • explicit implementation

Implicit implementation

In Implicit implementation is a regular way of implementation where we implement the methods as public in the child class
For example: suppose we have an interface ‘IBonus’ which has the method “Add()” refer below C# code snippet

public interface IBonus
{
    void Add();
}

Let’s implement it in a class refer C# code snippet below

public class Salary : IBonus
{
    public void Add()
    {
        Console.WriteLine("Added Bonus into salary.");
    }
}

Calling the above class

class Program
{
    static void Main()
    {
        Salary salary = new Salary();
        salary.Add();
    }
}

The above implementation is called Implicit implementation as we don’t need to specify anything. Simply we just inherited it, implemented its method, and calling the members.

Explicit implementation

In an explicit implementation, we need to use explicit names for accessing the methods. Because sometimes a class inheriting from two or more interface which has the same methods. So, in a class, we need to specify the name.

For example, consider the same example as mentioned above addition to that we will add one more interface ‘IOverTime’ which has the method “Add()” refer below C# code snippet

public interface IBonus
{
    void Add();
}

public interface IOverTime
{
    void Add();
}

Let’s inherit above two interfaces to a class, Now if we want to implement both methods from a class we have to prefix the interface name before the method name refer below C# code snippet

public class Salary : IBonus, IOverTime
{
    void IBonus.Add()
    {
        Console.WriteLine("Added Bonus into salary.");
    }

    void IOverTime.Add()
    {
        Console.WriteLine("Added Over Time into salary.");
    }
}

Let’s call this class from another program.

class Program
{
    static void Main()
    {
        Salary salary = new Salary();
        ((IBonus)salary).Add();
        ((IOverTime)salary).Add();
        Console.ReadLine();
    }
}

Output of the above code

Added Bonus into salary.
Added Over Time into salary.

In the above example, we can see as we have the same method declared in the class, but we can access both methods by its name.

Advantages of interface

  • It allows us to be more abstract.
  • It enforces class to implement functions.
  • It also specifies how to interact with the class
  • It supports multiple inheritances
  • Loose coupled code

Helpful resources


Leave a Reply

Your email address will not be published. Required fields are marked *