---
title: "interface in c# with example"  
description: "discus about interface"  
author: "Ravi Vishwakarma"  
published: 2021-07-02  
canonical: https://www.mindstick.com/articles/327192/interface-in-c-sharp-with-example  
category: "c#"  
tags: ["c#"]  
reading_time: 3 minutes  

---

# interface in c# with example

## Interface

Another way to achieve [abstraction](https://www.mindstick.com/blog/668/abstraction-and-encapsulation) in C#, is with interfaces. An interface is a completely [abstract class](https://www.mindstick.com/blog/665/constructor-in-abstract-class-in-c-sharp), which can only contain [abstract methods](https://www.mindstick.com/forum/159550/how-to-implement-an-interface-with-two-abstract-methods-by-a-lambda-expression) and [properties](https://www.mindstick.com/blog/673/properties) (with empty bodies). Acquiring the properties of one class in another class is called the inheritance. Inheritance provide reusability of code allowing extend an existing class and reduce the complexity of code. By default member of an interface are abstract and public. Interface can contain methods and properties but not fields. Interface must be inherited by another class to [implement an interface](https://www.mindstick.com/forum/161272/how-do-you-implement-an-interface-in-c-sharp) using colon (:) symbol. After implementing the inheritance method must be override. At the time of declaration of methods in interface, doesn’t contain the method body. The interface is created with interface keyword.

NOTE. Doesn’t use the override keyword at time of override the method

Syntax of inheritance.

Interface interface-name {

## // declare methods

[access specifier] [abstract] return-type function-name( [arguments] ) ;

[access specifier] [abstract] return-type function-name( [arguments] ) ;

// declare properties

}

## Syntax for implementing the interface

Class class-name : interface-name{

}

This colon say that implement an interface in class. If implement the interface then override the interface methods.

Example of declaration of interface

// interface declaration

Interface Vehicle {

// this is a methods in inheritance

[public void](https://www.mindstick.com/interview/2715/can-we-write-static-public-void-instead-of-public-static-void) applyBreaks();

}

## There are my type of interface

1. Single interface
2. Multiple interface

## Single interface

using System;

class HelloWorld {

static void Main() {

Bike bike=new Bike();

bike.printRecord();

bike.changeGear(2);

bike.printRecord();

bike.speedUp(10);

bike.printRecord();

bike.applyBreak();

bike.printRecord();

Vahicle bike=new Bike();

bike.printRecord();

bike.changeGear(2);

bike.printRecord();

bike.speedUp(10);

bike.printRecord();

bike.applyBreak();

bike.printRecord();

}

}

// declaring interface

interface Vahicle{

// declaring the methods in interface

void changeGear(int gear);

void applyBreak();

void speedUp(int up);

void printRecord();

}

// implementing the vehicle interface

class Bike : Vahicle{

private int gear;

private int up;

public Bike(){

}

public void changeGear(int gear){

this.gear=gear;

}

public void applyBreak(){

up--;

}

public void speedUp(int up){

this.up=up;

}

public void printRecord(){

Console.WriteLine('Gear '+this.gear+' Speed '+this.up);

}

}

Output :

Gear 0 Speed 0

Gear 2 Speed 0

Gear 2 Speed 10

Gear 2 Speed 9

Gear 0 Speed 0

Gear 2 Speed 0

Gear 2 Speed 10

Gear 2 Speed 9

## [Multiple inheritance](https://www.mindstick.com/forum/2519/java-multiple-inheritance)

\

using System;

class HelloWorld {

static void Main() {

Bike bike=new Bike();

bike.paintVahicle('\'red\'');

bike.printRecord();

bike.changeGear(2);

bike.printRecord();

bike.speedUp(10);

bike.printRecord();

bike.applyBreak();

bike.printRecord();

Car car=new Car() ;

car.paintVahicle('\'pink\'');

car.printRecord();

car.changeGear(5);

car.printRecord();

car.speedUp(40);

car.printRecord();

car.applyBreak();

car.printRecord();

}

}

// declaring vahicle interface

interface Vahicle{

// declaring the methods in interface

void changeGear(int gear);

void applyBreak();

void speedUp(int up);

void printRecord();

}

// declaring penter interface

interface Penter {

// declaring the new methods in penter interface

void paintVahicle(String color);

}

class Bike : Vahicle , Penter {

private int gear;

private int up;

private String paintColor ;

public Bike(){

}

public void changeGear(int gear){

this.gear=gear;

}

public void applyBreak(){

up--;

}

public void speedUp(int up){

this.up=up;

}

public void paintVahicle (String color)

{

this.paintColor=color;

}

public void printRecord(){

Console.WriteLine('Your bike color is '+this.paintColor+' Gear '+this.gear+' Speed '+this.up);

}

}

class Car : Vahicle , Penter {

private int gear;

private int up;

private String paintColor ;

public Car(){

}

public void changeGear(int gear){

this.gear=gear;

}

public void applyBreak(){

up=up-10;

}

public void speedUp(int up){

this.up=up;

}

public void paintVahicle (String color)

{

this.paintColor=color;

}

public void printRecord(){

Console.WriteLine('[Your car](https://www.mindstick.com/articles/23231/upgrade-your-car-with-maruti-genuine-accessories) color is '+this.paintColor+' Gear '+this.gear+' Speed '+this.up);

}

}

\

---

Original Source: https://www.mindstick.com/articles/327192/interface-in-c-sharp-with-example

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
