An interface in C# is defined using the interface keyword. It serves as a blueprint that declares methods, properties, events or indexers without implementation. A class or struct that implements the interface must provide the actual implementation, either implicitly or explicitly.
There are some important points to remember, as mentioned below:
- Interface members are public by default. While they are typically abstract, modern C# versions allow default implementations for methods.
- Interfaces cannot contain fields because they define a contract for behavior and do not store state.
- Interfaces support multiple inheritance, and a class can implement multiple interfaces using commas.
Example 1: Demonstrating the basic case for implementing the Interface.
using System;
interface inter1{
// method having only declaration with no definition
void display();
}
// Implementing inteface in Geeks
class Geeks : inter1{
// providing the body part of function
public void display(){
Console.WriteLine("Demonstration of interface");
}
public static void Main(String[] args){
Geeks t = new Geeks();
t.display();
}
}
Output
Demonstration of interface
Example 2: Implementation of interfaces and the use of polymorphism in C#
using System;
// interface declaration
interface Vehicle{
// abstract method.
void speedUp(int a);
}
// class implements interface
class Bicycle : Vehicle{
int speed;
// Method increase speed
public void speedUp(int increment){
speed = speed + increment;
}
// Method check speed
public void CheckSpeed(){
Console.WriteLine("speed: " + speed);
}
}
// class implements interface
class Bike : Vehicle{
int speed;
// to increase speed
public void speedUp(int increment){
speed = speed + increment;
}
public void CheckSpeed(){
Console.WriteLine("speed: " + speed);
}
}
class Geeks
{
public static void Main(String[] args)
{
// creating an instance of Bicycle doing some operations
Bicycle bicycle = new Bicycle();
bicycle.speedUp(3);
Console.WriteLine("Bicycle present state :");
bicycle.CheckSpeed();
// creating instance of bike.
Bike bike = new Bike();
bike.speedUp(4);
Console.WriteLine("Bike present state :");
bike.CheckSpeed();
}
}
Output
Bicycle present state : speed: 3 Bike present state : speed: 4
Advantages
- Loose coupling: It is used to achieve loose coupling rather than concrete implementations, we can decouple classes and reduce interdependencies.
- Abstraction: Interface helps to achieve full abstraction.
- Maintainability: It helps to achieve component-based programming that can make code more maintainable.
- Multiple Inheritance: Interface used to achieve multiple inheritance and abstraction.
- Define architecture: Interfaces add a plug and play like architecture into applications.
- Modularity: Promote a cleaner and more modular design. By separating the definition of behavior from the implementation
Abstract Class vs Interface
In C#, both abstract classes and interfaces are used to define contracts and enable polymorphism, but they differ in implementation capabilities and usage. Abstract classes can provide both behavior and declarations, while interfaces primarily define a contract that implementing classes must follow.
| Feature | Abstract Class | Interface |
|---|---|---|
| Implementation | Can have both method declarations and implementations | Only method/property/event/indexer declarations (except default implementations in C# 8.0+) |
| Access Modifiers | Can have private, protected, public members | Members are public by default; cannot have private fields |
| Fields | Allowed | Not allowed |
| Constructors | Allowed | Not allowed |
| Inheritance | Single inheritance | Multiple inheritance allowed |
| Use Case | Shared behavior across related classes | Contract or capability for multiple unrelated classes |