Interface in C#

Interface in C# with Examples

In this article, I am going to discuss one of the most important concepts i.e. Interface in C# with Examples. Please read our previous article where we discussed Inheritance in C#. At the end of this article, I am sure, you will understand what is Interface, why we need an interface, and how to use an interface in C# with real-time examples.

Why do we need an interface in C#?

We know the concept of multiple inheritances where one class is derived from more than one superclasses. For example a definition likes

class A: B, C

But this concept is not supported by .NET with classes. Since a large no of real-time applications require the use of multiple inheritances, where we inherit properties and behaviors from several different classes. That’s why .NET provides an alternative approach known as the interface to support the concept of multiple inheritances.

What is an interface in C#?

The Interface in C# is a fully un-implemented class used for declaring a set of operations of an object. So, we can define an interface as a pure abstract class which allows us to define only abstract methods. The abstract method means a method without a body or implementation.

An Interface in C# is used along with a class to define a contract which is an agreement on what the class will provide to an application. The interface defines what operations a class can perform. An interface declares the properties and methods. It is up to the class to define exactly what the method will do.

Interface in C# is a blueprint of a class. It is like an abstract class because all the methods of an interface are declared as abstract methods. It cannot have a method body and cannot be instantiated.

It is used to achieve multiple inheritances which can’t be achieved by class. It is used to achieve full abstraction because it cannot have a method body.

Its implementation must be provided by class or struct. The class or struct which implements the interface must provide the implementation of all the methods declared inside the interface.

What is the need for an interface when we have the abstract class to define abstract methods in C#?

.NET doesn’t support multiple inheritances with classes. So we must use interface as the superclass to develop abstraction for supporting multiple inheritances. If we define an abstract class in place of an interface, a service provider cannot implement multiple specifications so the service provider cannot have multiple businesses.

What are the different types of Inheritance?

A class can be inherited either from another class or from an interface also. So inheritance can be divided into two categories

  1. Implementation inheritance
  2. Interface inheritance

If a class is inheriting from another class we call it implementation inheritance and the main concept of implementation inheritance is child classes can consume the members of its parent class.

On the other hand, if a class is inheriting from an interface we call it Interface Inheritance but interface inheritance does not provide any reusability because here we are not consuming the members of the parent under the child. The child is only implementing the parent’s members.

How to declare an interface in C#?

By using the keyword interface we can declare an interface.

public interface InterfaceName
    //only abstract methods

// For example
public interface Example
    void show();

Here the keyword interface tells that Example is an interface containing one abstract method i.e. show(). By default, the members of an interface are public and abstract. An interface can contain

  1. Abstract methods
  2. Properties
  3. Indexes
  4. Events

An interface cannot contain

  1. Non-abstract functions
  2. Data fields
  3. Constructors
  4. Destructors
Should I use public access modifiers for interface methods?

.NET interface methods are implicitly public by default, even if they belong to nested interfaces. Non-public modifiers are not valid or necessary for interface methods. So the compiler will fail and warn you in this case. Nested interfaces may be declared protected or private but not the interface methods.

Can an interface implement an abstract class?

No. In .NET an interface cannot implement an abstract class. An interface may only extend a super interface. However, an abstract class can implement an interface because an abstract class can contain both abstract methods and concrete methods.

Can an interface be declared as sealed?

No, it is not permitted to declare an interface as sealed; it will cause a compilation error. This is a .NET language design decision. Interface types are intended to be implemented and can be extended without restriction.

Is more than one interface are allowed to implement a class?

Yes, a class can implement multiple interfaces; this is an effective way to achieve multiple inheritances in .NET. But a class can extend only one superclass.

Is it necessary to implement all interface methods?

It is not necessary for a class that implements an interface to implement all its methods, but in this case, the class must be declared as abstract.

How interface is different from a class in C#?

An interface is different from a class in the following ways:

  1. We cannot instantiate an interface.
  2. An interface does not contain any constructor or data fields or destructor, etc.
  3. All of the methods of an interface are abstract and public by default.
  4. An interface is not extended by a class; it is implemented by a class.
  5. An interface can extend multiple interfaces.
What are the similarities between the interface and abstract class in C#?

An interface is similar to an abstract class in the following ways

  1. Both interface and the abstract class cannot be instantiated means we cannot create the object.
  2. But we can create a reference variable for both interface and abstract class.
  3. The subclass should implement all abstract methods.
  4. Both cannot be declared sealed.
What is the main difference between interface and abstract class in C#?

The main difference to be answered in the interview is as follows. The interface is a fully un-implemented class used for declaring a set of operations of an object. The abstract class is a partially implemented class. It implements some of the operations of an object. These implemented operations are common for all next-level subclasses. The remaining operations are implemented by the next level subclasses according to their requirement.

The interface allows us to develop multiple inheritances. So we must start object design with interface whereas abstract class does not support multiple inheritances so it always comes next to interface in the object creation process.

What are the differences between the interface and abstract class in C#?
Abstract class Interface
It is a partially implemented class. It allows us to define both concrete and abstract methods. It is a fully un-implemented class. It allows us to define only abstract methods.
It provides both reusability and forcibility It provides the only forcibility
It should be declared as abstract by using the abstract keyword, abstract methods should also contain the abstract keyword. It should be created by using the keyword interface. Declaring its methods as abstract is optional because by default the methods of an interface are abstract. The compiler places abstract keywords at the time of program compilation.
A class that contains one or more abstract functions is called an abstract class. The class which contains all the abstract functions is known as an interface.
Its member’s default accessibility modifier is private and can be changed to any of the other accessibility modifiers. Its member’s default accessibility modifier is public and cannot be changed.
It is possible to declare data fields in an abstract class. But it is not possible to declare any data fields in an interface.
An abstract class can contain a non-abstract function. An interface cannot contain non-abstract functions.
An abstract class can inherit from another abstract class or from an interface. An interface can inherit from only other interfaces but cannot inherits from the abstract class.
It can have inner classes It can also have inner classes.
An abstract class cannot be used to implement multiple inheritances. An interface can be used to implement multiple inheritances.
Abstract class members can have access modifiers. Interface members cannot have access modifiers.
Rules to follow while working with the interface

While working with an interface, we must follow the below rules.

  1. The interface cannot have concrete methods, violation leads to CE: interface methods cannot have a body.
  2. We cannot declare interface members as private or protected members violation leads to CE:” modifier is not allowed here”.
  3. An interface cannot be instantiated but its reference variable can be created for storing its subclass object reference.
  4. We cannot declare the interface as sealed it leads to CE: “illegal combination of modifier interface and final”.
  5. The class derived from the interface should implement all abstract methods of the interface otherwise it should be declared as abstract else it leads to a compile-time error.
  6. The subclass should implement the interface method with a public keyword because the interface methods default accessibility modifier is public.
  7. In an interface, we cannot create fields variable violation leads to a compile-time error.

We have discussed a lot of theories. Let’s move towards practical examples.

Example to Understand Interfaces in C#
namespace InterfaceDemo
    public interface A
        void method1();
        void method2();
    interface B : A
        void method3();
    class MyClass : B
        public void method1()
            Console.WriteLine("implement method1");
        public void method2()
            Console.WriteLine("implement method2");
        public void method3()
            Console.WriteLine("implement method3");
    class Program
        static void Main(string[] args)
            MyClass obj = new MyClass();
            Console.WriteLine("Press any key to exist.");

Interface in C#

namespace InterfaceDemo
    public interface Area
        void area(double a, double b);
    class Rectangle : Area
        public void area(double a, double b)
            double areaRectangle;
            areaRectangle = a * b;
            Console.WriteLine("the area of rectangle is :" + areaRectangle);
    class Circle : Area
        static double PI = 3.14;
        public void area(double a, double b)
            double areaCircle;
            areaCircle = PI * a * a;
            Console.WriteLine("the area of Circle is :" + areaCircle);
    class Program
        static void Main(string[] args)
            Area a = new Rectangle();
            a.area(5, 6);
            a = new Circle();
            a.area(7, 0);
            Console.WriteLine("Press any key to exist.");

Interface in C#

Note: Interface methods in C# are by default public and abstract. We cannot explicitly use the public and abstract keywords for an interface method.

Advantages of Interface in C#:
  1. It is used to achieve loose coupling.
  2. It is used to achieve total abstraction.
  3. To achieve component-based programming
  4. To achieve multiple inheritance and abstraction.
  5. Interfaces add a plug-and-play-like architecture to applications.

In the next article, I am going to discuss Multiple Inheritance using Interfaces in C# with Examples. Here, in this article, I try to explain the Interface in C# with Examples. I hope this article will help you with your need. I would like to have your feedback. Please post your feedback, question, or comments about this Interface in C# with the Examples article.

2 thoughts on “Interface in C#”

  1. blank

    Hello, when i run this prog, by step by step, the prog work fine. But, for better understanding, I was disappointed that it does not pass inside the interface… Thank you for this tutorial, I will continue the tutorial.

Leave a Reply

Your email address will not be published.