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

Java Program to find the sum of the Largest Forward Diagonal

in this tutorial, we will write a java program to find the sum of the Largest Forward Diagonal in an Arraylist (matrix). Java Program to …

C Program to search an element in an array using Pointers

A separate function( search_function()) will be created where the array pointer will be declared and the searched element along with the size of an array …

C Program to find the sum of the digits of a number using recursion function

This C program calculates the sum of digits of a given number using recursion. Here’s a concise explanation: Function Definition: sumDigits(int n) This function calculates …

C program to find factorial of a number using Ternary operator with Recursion

Recursion refers to the function calling itself directly or in a cycle. Before we begin, you should have the knowledge of following in C Programming: …

C Program to Add Two Numbers Using Call by Reference

The program takes the two numbers from the user and passes the reference to the function where the sum is calculated. You may go through …

Find the output ab, cd, ef, g for the input a,b,c,d,e,f,g in Javascript and Python

In this tutorial, we will write a program to find a pairs of elements from an array such that for the input [a,b,c,d,e,f,g] we will …