C# Interface2 min read

An Interface is the same as a class, it is a blueprint of a class. Just like any other class, an interface can have methods, properties, events, and indexers as its members. But unlike a class, the method in it is an abstract method (only method signature, contain no body).

  • Interface is used to achieve abstraction and multiple inheritance which cannot be achieved through the class.
  • An interface specifies what a class must do but not how to hence the abstraction.
  • By deafult, the members of interface are public and abstract. It cannot have private members.

Declaring an interface

The keyword interface is used to declare an interface and is done just like a class as shown below.

Example:

Implementation of an interface is done in the following way on a class.


Example 1: C# program to illustrate interface

Output:

The dog barks: woof woof


Multiple Interface

We can create multiple interfaces in a program having different tasks and use them in the same class. And to implement multiple interfaces in the same class we separate them with commas in between as shown in the program below.

Example 2: C# program to illustrate multiple interface

Output:

The Dog barks.
The Cat Meows.


MORE

Keith Number Program in C

In this tutorial, we will write a Keith number in C. It is one of the most asked questions in an interview. Before that, you …
Read More

Keith Number Program in C++

In this tutorial, we will write a Keith number in C++. It is one of the most asked questions in an interview. Before that, you …
Read More

Singly linked list Program C++

In this tutorial, we will write a C++ program to implement singly linked list. In this program, we will create a separate function for each …
Read More

Fascinating Number Program in C

In this tutorial, we will write a C program to check whether the given number is a Fascinating number or not. You may go through …
Read More

Peterson Number in C

In this tutorial, we will learn about the Peterson number and check if the number is Peterson or not in C. What is Peterson Number? …
Read More

Peterson Number in C++

In this tutorial, we will learn about the Peterson number and write a C++ program to check if the given number is a Peterson number …
Read More