Sitemap
Yashdesai

Yashdesai

Interface

3 min readSep 22, 2024

--

What is an Interface in C#?

The Interface in C# is a Fully Unimplemented Class used for declaring a set of operations/methods 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. It is used to achieve multiple inheritances, which the class can’t achieve.

It is used to achieve full abstraction because it cannot have a method body.

Interfaces allow you to define a common set of functionality that multiple classes can share, promoting code reusability and ensuring a consistent structure for related classes.

  1. Class: Contains only the Non-Abstract Methods (Methods with Method Body).
  2. Abstract Class: Contains both Non-Abstract Methods (Methods with Method Body) and Abstract Methods (Methods without Method Body).
  3. Interface: Contain only Abstract Methods (Methods without Method Body).

class has another class as a Parent, a class can also have an Interface as a Parent. If a class has an interface as a Parent, the class is responsible for implementing all the abstract methods of the interface. For a better understanding, please have a look at the below diagram

the Parent Interface imposes restrictions on the Child Classes. What restrictions? The restrictions are to implement each and every method of the interface under the child class.

we cannot declare fields/variables, constructors, and destructors in an interface in C#.

Interfaces cannot contain fields

What are the Members we can and can’t define in an interface in C#?

Get Yashdesai’s stories in your inbox

Join Medium for free to get updates from this writer.

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

Abstract Class:

  1. It is a partially implemented class. It allows us to define both concrete and abstract methods.
  2. It should be declared as abstract by using the abstract keyword, abstract methods should also contain the abstract keyword.
  3. Its member’s default accessibility modifier is private and can be changed to any of the other accessibility modifiers.
  4. It is possible to declare data fields in an abstract class.
  5. An abstract class can contain a non-abstract function.
  6. An abstract class can inherit from another abstract class or from an interface.
  7. An abstract class cannot be used to implement multiple inheritances.
  8. Abstract class members can have Access Specifiers.

Interface:

  1. It is a fully un-implemented class. It allows us to define only abstract methods.
  2. It should be created by using the keyword interface. By default, all the members are abstract only. Explicitly using abstract keyword is not allowed.
  3. Its member’s default accessibility modifier is public and cannot be changed.
  4. It is not possible to declare any data fields in an interface.
  5. An interface cannot contain non-abstract functions.
  6. An interface can inherit from only other interfaces but cannot inherits from the abstract class.
  7. An interface can be used to implement multiple inheritances.
  8. Interface members cannot have Access Specifiers.

--

--