An interface in defines a contract that specifies the members a class or struct must implement. It is used to achieve abstraction and allows different classes to provide their own implementations of the same functionality.
- Interfaces are declared using the interface keyword and can contain methods, properties, events, and indexers.
- A class or struct can implement multiple interfaces, supporting multiple inheritance of behavior contracts.
Example: Program to demonstrates how a class implements an interface and defines the required method.
using System;
// Declare an interface
interface IAnimal{
void Sound();
}
// Implement the interface
class Dog : IAnimal{
public void Sound() { Console.WriteLine("Dog barks"); }
}
class Geeks{
static void Main(){
// Create an object of Dog
Dog dog = new Dog();
// Call the implemented method
dog.Sound();
}
}
Output
Dog barks
Explanation: The IAnimal interface declares the Sound() method. The Dog class implements this method and provides its own behavior.
Syntax
interface InterfaceName{
// Method declaration
void Display();
}// Class implementing the interface
class ClassName : InterfaceName{
public void Display()
{
Console.WriteLine("Hello");
}
}
Here, InterfaceName declares the Display() method, and ClassName provides its implementation using the : operator.
How Interfaces Works
An interface defines what operations a class must support, while the implementing class determines how those operations work.

- An interface declares the required members.
- A class implements the interface using the : operator.
- The implementing class provides the required methods or properties.
- Objects of different classes can be accessed through the same interface type.
Multiple Interfaces
C# does not support multiple inheritance of classes, but a class can implement multiple interfaces. This allows a class to provide different sets of functionality.
Example: The following program demonstrates a class implementing two interfaces.
using System;
// First interface
interface IPrintable{
void Print();
}
// Second interface
interface IScannable{
void Scan();
}
// Implement multiple interfaces
class Printer : IPrintable, IScannable{
public void Print(){
Console.WriteLine("Printing document");
}
public void Scan(){
Console.WriteLine("Scanning document");
}
}
class Geeks{
static void Main()
{
// Create Printer object
Printer printer = new Printer();
// Call interface methods
printer.Print();
printer.Scan();
}
}
Output
Printing document Scanning document
Explanation: The Printer class implements both IPrintable and IScannable, providing implementations of their respective methods.
Types of Interface Members
Interfaces can declare different types of members to define a contract.
- Methods: Define operations that implementing types must support.
- Properties: Define how values can be accessed or modified.
- Events: Define notifications that implementing types can expose.
- Indexers: Allow objects to be accessed using index notation.
Modern C# also supports members with default implementations, as well as static and certain private interface members.
Interface Inheritance
An interface can inherit from one or more interfaces. A class implementing a derived interface must satisfy the contracts of its inherited interfaces.
using System;
// Base interface
interface IAnimal{
void Eat();
}
// Derived interface
interface IDog : IAnimal{
void Bark();
}
// Implement derived interface
class Dog : IDog {
public void Eat(){
Console.WriteLine("Dog is eating");
}
public void Bark(){
Console.WriteLine("Dog is barking");
}
}
class Geeks{
static void Main(){
// Create Dog object
Dog dog = new Dog();
dog.Eat();
dog.Bark();
}
}
Output
Dog is eating Dog is barking
Explanation: The IDog interface inherits Eat() from IAnimal and declares Bark(). Therefore, Dog must implement both methods.
Default Interface Methods
Starting with C# 8.0, interfaces can contain methods with default implementations. These allow shared behavior to be defined without requiring every implementing class to provide its own implementation.
Example:
using System;
interface IMessage{
// Default interface method
void Show(){
Console.WriteLine("Default Message");
}
}
class Message : IMessage{
// Uses the default implementation
}
class Geeks{
static void Main() {
// Access through interface reference
IMessage msg = new Message();
msg.Show();
}
}
Output
Default Message
Explanation: The Message class uses the default implementation of Show() provided by IMessage.
Difference Between Interface and Abstract Class
Interface | Abstract Class |
|---|---|
Defines a contract for implementing types. | Defines a base class with shared behavior and state. |
A class can implement multiple interfaces. | A class can inherit only one base class. |
Cannot contain instance fields. | Can contain instance fields. |
Cannot define instance constructors. | Can define constructors. |
Supports default method implementations in modern C#. | Supports abstract and concrete methods. |
Advantages of Interfaces
- Abstraction: Defines required functionality without exposing implementation details.
- Multiple Implementation: Allows a class to implement multiple contracts.
- Loose Coupling: Reduces dependency on specific classes.
- Code Maintainability: Makes implementations easier to replace or extend.
- Testability: Supports mocking and dependency injection in applications.
When to Use Interfaces
Interfaces are useful when multiple classes need to follow the same contract while providing different implementations.
For example, in a payment application, an IPayment interface can declare a Pay() method. Classes such as CreditCardPayment and UPIPayment can implement this method differently. This allows the application to work with different payment methods through the same interface.