C# | Interface
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
// interface
using
System;
// A simple interface
interface
inter1
{
// method having only declaration
// not definition
void
display();
}
// A class that implements interface.
class
testClass : inter1
{
// providing the body part of function
public
void
display()
{
Console.WriteLine(
"Sudo Placement w3wiki"
);
}
// Main Method
public
static
void
Main (String []args)
{
// Creating object
testClass t =
new
testClass();
// calling method
t.display();
}
}
Output:Sudo Placement w3wiki
- Example 2:
// C# program to illustrate the interface
using
System;
// interface declaration
interface
Vehicle {
// all are the abstract methods.
void
changeGear(
int
a);
void
speedUp(
int
a);
void
applyBrakes(
int
a);
}
// class implements interface
class
Bicycle : Vehicle{
int
speed;
int
gear;
// to change gear
public
void
changeGear(
int
newGear)
{
gear = newGear;
}
// to increase speed
public
void
speedUp(
int
increment)
{
speed = speed + increment;
}
// to decrease speed
public
void
applyBrakes(
int
decrement)
{
speed = speed - decrement;
}
public
void
printStates()
{
Console.WriteLine(
"speed: "
+ speed +
" gear: "
+ gear);
}
}
// class implements interface
class
Bike : Vehicle {
int
speed;
int
gear;
// to change gear
public
void
changeGear(
int
newGear)
{
gear = newGear;
}
// to increase speed
public
void
speedUp(
int
increment)
{
speed = speed + increment;
}
// to decrease speed
public
void
applyBrakes(
int
decrement){
speed = speed - decrement;
}
public
void
printStates()
{
Console.WriteLine(
"speed: "
+ speed +
" gear: "
+ gear);
}
}
class
GFG {
// Main Method
public
static
void
Main(String []args)
{
// creating an instance of Bicycle
// doing some operations
Bicycle bicycle =
new
Bicycle();
bicycle.changeGear(2);
bicycle.speedUp(3);
bicycle.applyBrakes(1);
Console.WriteLine(
"Bicycle present state :"
);
bicycle.printStates();
// creating instance of bike.
Bike bike =
new
Bike();
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:
Contact Us