Interface in c#

Interface defines signature for other classes. Interface is look like class but there is no any definition for any method in the interface. Interface is a contract or signature. When we implement class from interface we have to define implementation for every members of interface. Members of interface don’t have any access modifier.

Please see below code for declaration of interface:

interface IEmployee
{

}

In this interface we can define only declaration of members like events, methods or properties.

If we assign any access modifier for interface member it gives compile time error.

public interface IEmployee
{
   private int getEmployee();
}

Error:
The modifier ‘private’ is not valid for this item.

For removing this error we have to remove access modifier ‘private’

interface IEmployee
{
    string getEmployee();

    int insertEmployee();
}

You can implement the interface in another class by using (:) colon like below code snippet:

public interface IEmployee
{
    int getEmployee();
}

class Employee : IEmployee
{
    int EmployeeId = 123;

    public int getEmployee()
    {
        return EmployeeId;
    }
}

In the above code I have created one interface where I declared one method. In the employee class if not implement the “getEmployee” method it gets compile time error. So I have implemented this method in derived class. If I take another class and it also derived from interface in that class also we have to implement “getEmployee” method.

See below code snippet for more understanding

using System;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            Employee Emp = new Employee();

            Console.Write(Emp.getEmployee() + "\n");

            Console.ReadLine();
        }
    }

    public interface IEmployee
    {
        int getEmployee();
    }

    class Employee : IEmployee
    {
        int EmployeeId = 123;

        public int getEmployee()
        {
            return EmployeeId;
        }
    }
}

Output:
123

Interface can also derive from another interface means like multiple inheritance concept see below code snippet.

using System;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            Employee Emp = new Employee();

            Emp.getEmployee();

            Console.Write(Emp.displayEmployee() + "\n");

            Console.ReadLine();
        }
    }

    public interface IDisplayEmployee
    {
        int displayEmployee();
    }

    public interface IGetEmployee
    {
        void getEmployee();
    }

    class Employee : IDisplayEmployee, IGetEmployee
    {
        int EmployeeId = 0;

        public void getEmployee()
        {
            EmployeeId =123;
        }

        public int displayEmployee()
        {
            return EmployeeId;
        }
    }
}

Output:
123

We can also create instance like this below code snippet:

IGetEmployee emp = new Employee();

emp.getEmployee();

But in this code we cannot access method of another interface.

And one more method for accessing method in interface is like below:

Employee employee = new Employee();

 ((IDisplayEmployee)employee).getEmployee();
  

Key Points:
1.       Interface cannot contain fields means we cannot declare any variable in interface
2.       Interface cannot contain constructors and Destructors.
3.       We cannot create the instance of interface. It gives compile time error.
4.       Interface can also derive from another interface.
5.       Only declaration of members allowed in interface.
6.       Also possible method overloading using interface.
7.       Any modifier is not supported for interface members (static also).
8.       C# ideally not supports for multiple inheritances by using interface we can possible it.





Post a Comment

0 Comments