Monday, November 1, 2021

C# Interface



What is an interface? Interface literally means a medium for working between two things, such as a user interface. When we say user interface, it is the interface between the user and the system, allowing user to interact with the system.

In Object Oriented Programming (OOP), interface is a kind of contract between Interface type and the Class type. Interface is a type like class but interface contains only methods and properties. Unlike class, interface does not have any field. The methods in the interface are always public and abstract. Interface represents abstract behaviors which are implemented by the contracting class. The contracting class is the class that implements the interface.

In C#, we know that although a class can inherit another class, a class cannot inherit more than one class. In other words, C# does not support multiple inheritance. But C# allows to inherit multiple interfaces by a single class instead of inheritance of multiple classes. 
Although a class can inherit only one other class, but a class can inherit one or more interfaces. 
A class can inherit another class and multiple interfaces at the same time.

Class vs Interface
  • A class can have field but an interface cannot have field.
  • A class can have abstract , non abstract or any other kind of methods but interface has all its methods abstract and public methods.
  • Interface provides contract and class implements that contract.
  • Interface methods are abstract but 'abstract' modifier are not used with them but a class with abstract method must have abstract modifier with the method and the class.
  • Instance of a non abstract class can be created but instance of interface can never be created. (Because the methods of interface are always abstract.) 
  • A class can inherit an interface but not vice versa.
  • When a class inherits an interface, it cannot consume the interface directly. The class must, first of all, implement all the methods of the interface, only then the class can consume the methods of the interface. 
  • But when a class inherits another class, it is not necessarily required to implement the methods of the parent class. If the parent class has virtual methods, they can or cannot be overridden by the child class.  The virtual implies that the method is override-able in the child class. The child class can consume the parent class's methods directly and override them if needed. A parent class method without virtual modifier cannot be overridden by the child class.
So, when a class inherits another class it can consume the methods of its parent class directly or by overriding the methods, in other words, it can be re-implemented but in case of interface whenever a class inherits an interface, it must always implement the methods of the interface because the methods within the interface are abstract. They do not have any implementation within the interface, so whenever the class inherits the interface, it must implement the methods of that interface. Implementation of the method of interface by the inheriting class is mandatory. For example, to consume the InterfaceEx interface, the AddClass class implements all the methods of the InterfaceEx interface. Inheriting class cannot consume the interface methods unless and until all methods of the interface are implemented by the consuming class.

namespace InterfaceEx
{
    interface MyInterface
    {
        int Calculate(int num1, int num2);
        void DisplayResult(int result);
    }
}

using System;

namespace InterfaceEx
{
    class AddClass : MyInterface
    {
        public int Calculate(int num1, int num2)
        {
            return num1 + num2;
        }

        public void DisplayResult(int result)
        {
            Console.WriteLine("Sum of two integers: {0}", result.ToString());
        }
    }
}
The Program class is a client class that eventually consumes the AddClass class.

using System;

namespace InterfaceEx
{
    class Program
    {
        static void Main(string[] args)
        {
            AddClass c1 = new AddClass();
            c1.DisplayResult(c1.Calculate(20, 40));
            Console.ReadLine();
        }
    }
}
Some Facts about Interface
  • When a class inherits an interface, it must implement all the methods of the interface, it cannot consume the interface without implementing its all methods.
  • All the methods of interface are public and abstract but the explicit use of public and abstract keywords during method declaration is prohibited with the methods of interface.
  • There is no field in an interface.
  • Only public or internal access modifiers can be used with a class. Similarly, public or internal access modifiers can be used with interfaces.
  • One interface can inherit another interface. It means that methods of parent Interface will be inherited by the child Interface. Two interfaces can have methods of the same name with the same method signature. When a class implements both these interfaces then it has to adopt the methods.

No comments:

Post a Comment

Hot Topics