Like a class, Interface can have methods, properties, events, and indexers as its members. But interfaces will contain only the declaration of the members. The implementation of the interface’s members will be given by class who implements the interface implicitly or explicitly.
- Interfaces specify what a class must do and not how.
- Interfaces can’t have private members.
- By default all the members of Interface are public and abstract.
- The interface will always defined with the help of keyword ‘interface‘.
- Interface cannot contain fields because they represent a particular implementation of data.
- Multiple inheritance is possible with the help of Interfaces but not with classes.
Syntax for Interface Declaration:
interface <interface_name >
{
// declare Events
// declare indexers
// declare methods
// declare properties
}
Syntax for Implementing Interface:
class class_name : interface_name
To declare an interface, use interface keyword. It is used to provide total abstraction. That means all the members in the interface are declared with the empty body and are public and abstract by default. A class that implements interface must implement all the methods declared in the interface.
- Example 1:
// C# program to demonstrate working of// interfaceusingSystem;// A simple interfaceinterfaceinter1{// method having only declaration// not definitionvoiddisplay();}// A class that implements interface.classtestClass : inter1{// providing the body part of functionpublicvoiddisplay(){Console.WriteLine("Sudo Placement GeeksforGeeks");}// Main MethodpublicstaticvoidMain (String []args){// Creating objecttestClass t =newtestClass();// calling methodt.display();}}Output:Sudo Placement GeeksforGeeks
- Example 2:
// C# program to illustrate the interfaceusingSystem;// interface declarationinterfaceVehicle {// all are the abstract methods.voidchangeGear(inta);voidspeedUp(inta);voidapplyBrakes(inta);}// class implements interfaceclassBicycle : Vehicle{intspeed;intgear;// to change gearpublicvoidchangeGear(intnewGear){gear = newGear;}// to increase speedpublicvoidspeedUp(intincrement){speed = speed + increment;}// to decrease speedpublicvoidapplyBrakes(intdecrement){speed = speed - decrement;}publicvoidprintStates(){Console.WriteLine("speed: "+ speed +" gear: "+ gear);}}// class implements interfaceclassBike : Vehicle {intspeed;intgear;// to change gearpublicvoidchangeGear(intnewGear){gear = newGear;}// to increase speedpublicvoidspeedUp(intincrement){speed = speed + increment;}// to decrease speedpublicvoidapplyBrakes(intdecrement){speed = speed - decrement;}publicvoidprintStates(){Console.WriteLine("speed: "+ speed +" gear: "+ gear);}}classGFG {// Main MethodpublicstaticvoidMain(String []args){// creating an instance of Bicycle// doing some operationsBicycle bicycle =newBicycle();bicycle.changeGear(2);bicycle.speedUp(3);bicycle.applyBrakes(1);Console.WriteLine("Bicycle present state :");bicycle.printStates();// creating instance of bike.Bike bike =newBike();bike.changeGear(1);bike.speedUp(4);bike.applyBrakes(3);Console.WriteLine("Bike present state :");bike.printStates();}}Output:Bicycle present state : speed: 2 gear: 2 Bike present state : speed: 1 gear: 1
Advantage of Interface:


