Interface
Another way to achieve abstraction in C#, is with interfaces. An interface is a completely abstract class, which can only contain abstract methods and 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 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 applyBreaks();
}
There are my type of interface
- Single interface
- 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
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 color is '+this.paintColor+' Gear '+this.gear+' Speed '+this.up);
}
}