Interfaces In .NET

Introduction

In the article before this, I explained Abstract Classes.

Interfaces can contain only abstract members in it.

Non Abstract Class in C#

Only Non abstract members

Abstract Class in C#

Both Abstract and Non abstract members

What is Interface?

Only Abstract Members.

An Interface can be used for 2 reasons.

  1. Development of a distributed application
  2. They support multiple inheritance

A class can have only one immediate parent class, whereas it can have any number of interfaces as its parent.

An Inheritance can be one of 2 categories.

  1. Implementation Inheritance
  2. Interface Inheritance

An inheritance that is achieved by inheriting from a class is implementation inheritance. This is single under Java and .Net languages.

Inheritance achieved by inheriting from an interface is interface inheritance. This is multiple in all languages.

Interface1.gif

  1. Default scope for members of an interface is public.
  2. Every member of an interface, by default, is an abstract.
  3. Interface can't contain variable declarations in it.
  4. As a class can inherit from another class or interface, in the same way, an interface can inherit from another interface but not a class.

Add the interface under the project as inter1.cs and write the following.

using System;
public class interclass: inter1, inter2
{
    public void add(int x, int y)
    {
        Console.WriteLine(x + y);
    }
    public void sub(int x, int y)
    {
        Console.WriteLine(x - y);
    }
    public void mul(int x, int y)
    {
        Console.WriteLine(x * y);
    }
    public void div(int x, int y)
    {
        Console.WriteLine(x / y);
    }
    void inter1.test()
    {
        Console.WriteLine("method of interface1");
    }
    void inter2.test()
    {
        Console.WriteLine("method of interface2");
    }
    public static void Main()
    {
        interclass c = new interclass();
        c.add(1, 2);
        c.sub(2, 2);
        c.mul(1, 2);
        c.div(4, 2);
        inter1 i1 = c;
        inter2 i2 = c;
        i1.test();
        i2.test();
        Console.ReadLine();
    }
}

Earlier, we have discussed multiple inheritance; it is not supported in Java and .Net languages because of ambiguity problems but in the case of an interface, we do not have an ambiguity problem even if the same method is defined under multiple interfaces and implemented in a class. The problem in an interface can be resolved in two ways.

  1. Implement each method separately for each interface underclass by prefixing the method with interface name, but while calling the method we need to call only the method with only a reference of the interface as we have done in our example.
  2. Implement a method only under a class where each interface thinks its own method is implemented. In this case, we also& can also invoke a method directly by using the object of the class.

If we do not want to implement using an interface name, then.

using System;
public class interclass : inter1, inter2
{
    public void add(int x, int y)
    {
        Console.WriteLine(x + y);
    }
    public void sub(int x, int y)
    {
        Console.WriteLine(x - y);
    }
    public void mul(int x, int y)
    {
        Console.WriteLine(x * y);
    }
    public void div(int x, int y)
    {
        Console.WriteLine(x / y);
    }
    void inter1.test()
    {
        Console.WriteLine("method of interface1");
    }
    void inter2.test()
    {
        Console.WriteLine("method of interface2");
    }
    public void test()
    {
        Console.WriteLine("declare under multiple methods");
    }
    public static void Main()
    {
        interclass c = new interclass();
        c.add(1, 2);
        c.sub(2, 2);
        c.mul(1, 2);
        c.div(4, 2);
        inter1 i1 = c;
        inter2 i2 = c;
        i1.test();
        i2.test();
        c.test(); // Call to the test method declared under the Main method
        Console.ReadLine();
    }
}


Similar Articles