LATEST 🚀 Welcome to Upgradix by Shrash Studio – Your destination for free technology learning and career growth.
🕘 Updated:
C# Programming

SOLID - Interface Segregation Principle

ISP says that a class should not be forced to implement methods it does not need. Instead of one large interface, create smaller interfaces based on specific responsibilities.

How to Apply the Interface Segregation Principle in C#

Quick Answer: The Interface Segregation Principle says that clients should not depend on methods they do not use. Instead of one large interface, create smaller interfaces around specific responsibilities. Each class should implement only the operations it actually needs.

S1. Why Do We Need the Interface Segregation Principle?

Imagine you manage a school with 1,000 staff members. Teachers teach classes, accountants manage fees, and drivers operate school buses.

Would you give every staff member one form containing every possible task? A teacher does not need bus-driving operations. A driver does not need fee-management operations.

Software interfaces can create the same problem. Let's first see what happens without this principle.

S2. The Problem: One Large Interface

Interface = a contract that tells a class which methods it must provide.

Imagine that the school application uses one interface for every staff role. It contains teaching, fee collection and bus operations.

Example 1: A large interface used by every school employee

This example compiles, but some methods do not belong to each role.

using System;

public interface ISchoolStaff
{
    void TeachClass();
    void CollectFees();
    void DriveSchoolBus();
}

public class Teacher : ISchoolStaff
{
    public void TeachClass()
    {
        Console.WriteLine("Teacher is teaching Class 8.");
    }

    public void CollectFees()
    {
        throw new NotSupportedException();
    }

    public void DriveSchoolBus()
    {
        throw new NotSupportedException();
    }
}

public class Accountant : ISchoolStaff
{
    public void TeachClass()
    {
        throw new NotSupportedException();
    }

    public void CollectFees()
    {
        Console.WriteLine("Accountant collected the school fee.");
    }

    public void DriveSchoolBus()
    {
        throw new NotSupportedException();
    }
}

public class SchoolBusDriver : ISchoolStaff
{
    public void TeachClass()
    {
        throw new NotSupportedException();
    }

    public void CollectFees()
    {
        throw new NotSupportedException();
    }

    public void DriveSchoolBus()
    {
        Console.WriteLine("Driver started the school bus.");
    }
}

public class Program
{
    public static void Main()
    {
        ISchoolStaff teacher = new Teacher();
        ISchoolStaff accountant = new Accountant();
        ISchoolStaff driver = new SchoolBusDriver();

        teacher.TeachClass();
        accountant.CollectFees();
        driver.DriveSchoolBus();
    }
}

Expected output

Teacher is teaching Class 8.
Accountant collected the school fee.
Driver started the school bus.

This code runs, but it gets hard to manage. Each class must provide methods it cannot use.

What goes wrong?

  1. Unneeded methods: Teachers receive fee and bus operations they never use.
  2. Fake implementations: Classes may throw exceptions for unsupported methods.
  3. Risky changes: Adding a method affects every class that implements the interface.
  4. Harder testing: Tests must account for methods that do not belong to the class.
  5. Poor clarity: The interface does not explain each role clearly.

Now think about 1,000 staff members and many different roles. A single large interface becomes harder to maintain.

Remember: The problem is not the number of methods alone. The real problem is forcing unrelated classes to depend on methods they do not need.

Animation 1: How the large interface creates unwanted dependencies

One interface, three unrelated responsibilities

The red highlights show that every role receives the same large contract.

📚TeacherNeeds teachingDoes not need fees or bus
🧾AccountantNeeds fee collectionDoes not need teaching or bus
🚌Bus driverNeeds bus operationDoes not need teaching or fees
 
ISchoolStaff TeachClass() · CollectFees() · DriveSchoolBus()
Unwanted dependency

The animation highlights the shared interface and the unrelated methods forced on each staff role.

S3. The Solution: Split the Interface by Responsibility

The Interface Segregation Principle gives us a simpler design.

Segregation = splitting something into smaller, focused parts.

Instead of one large interface, create three small interfaces. Each interface describes one school responsibility.

  • ITeaching contains teaching operations.
  • IFeeCollection contains fee operations.
  • ISchoolBusOperation contains bus operations.

Now each staff class implements only the interface it needs.

Example 2: Separate interfaces for school responsibilities

Assumption: This example uses standard C# syntax supported by modern .NET versions.

using System;

public interface ITeaching
{
    void TeachClass();
}

public interface IFeeCollection
{
    void CollectFees();
}

public interface ISchoolBusOperation
{
    void DriveSchoolBus();
}

public class Teacher : ITeaching
{
    public void TeachClass()
    {
        Console.WriteLine("Teacher is teaching Class 8.");
    }
}

public class Accountant : IFeeCollection
{
    public void CollectFees()
    {
        Console.WriteLine("Accountant collected the school fee.");
    }
}

public class SchoolBusDriver : ISchoolBusOperation
{
    public void DriveSchoolBus()
    {
        Console.WriteLine("Driver started the school bus.");
    }
}

public class Program
{
    public static void Main()
    {
        ITeaching teacher = new Teacher();
        IFeeCollection accountant = new Accountant();
        ISchoolBusOperation driver = new SchoolBusDriver();

        teacher.TeachClass();
        accountant.CollectFees();
        driver.DriveSchoolBus();
    }
}

Expected output

Teacher is teaching Class 8.
Accountant collected the school fee.
Driver started the school bus.

Notice the difference. No class needs to throw an exception for an unrelated operation.

Animation 2: The segregated interface design

One responsibility for each interface

Each role connects only to the contract it needs.

📚TeacherTeaching role
🧾AccountantFee role
🚌Bus driverTransport role
 
 
 
ITeachingTeachClass()
IFeeCollectionCollectFees()
ISchoolBusOperationDriveSchoolBus()
Focused dependency

The animation highlights each staff role and its own small interface contract.

Compare both approaches

Large interface compared with segregated interfaces
Without ISP With ISP
One large ISchoolStaff interface. Three focused interfaces.
Teacher implements fee and bus methods. Teacher implements only ITeaching.
Unsupported methods may throw exceptions. Unrelated methods are not required.
New methods affect many classes. Changes stay closer to the relevant contract.
Responsibilities are mixed. Responsibilities are clearly separated.

The idea to remember

  • Interface = a contract that a class agrees to follow.
  • Segregation = splitting a large contract into focused parts.
  • Client = a class or service that uses an interface.
  • ISP = clients should not depend on methods they do not use.
  • Implementation = the code that performs the required operation.

S4. Step-by-Step Code Breakdown

1. Create small interfaces

ITeaching describes only teaching work. It does not mention fees or transport.

IFeeCollection describes fee collection. It stays separate from classroom work.

ISchoolBusOperation describes the bus operation. It does not include unrelated tasks.

2. Implement only the required interface

The Teacher class implements ITeaching. It does not implement the other two interfaces.

The Accountant class implements IFeeCollection. The bus driver implements ISchoolBusOperation.

3. Use the interface as the reference type

The program stores each object through its relevant interface. This keeps the caller focused on the required operation.

For example, the teacher variable exposes TeachClass(). It does not expose fee collection or bus operations.

4. Add a role that needs more than one responsibility

Some staff members may perform multiple duties. A class can implement more than one small interface.

Example 3: A school coordinator with two responsibilities

using System;

public interface ITeaching
{
    void TeachClass();
}

public interface IFeeCollection
{
    void CollectFees();
}

public class SchoolCoordinator : ITeaching, IFeeCollection
{
    public void TeachClass()
    {
        Console.WriteLine("Coordinator is teaching a class.");
    }

    public void CollectFees()
    {
        Console.WriteLine("Coordinator collected the school fee.");
    }
}

public class Program
{
    public static void Main()
    {
        var coordinator = new SchoolCoordinator();

        coordinator.TeachClass();
        coordinator.CollectFees();
    }
}

Expected output

Coordinator is teaching a class.
Coordinator collected the school fee.

This is still ISP-friendly. The coordinator needs both responsibilities, so it implements both contracts.

S5. Architecture and Real-World Trade-Offs

ISP helps keep application boundaries clear. It is useful in layered systems, APIs and microservices.

How the design fits an application

Imagine a school management application with separate services for teaching, fees and transport. Each service can depend on the contract it actually needs.

A focused dependency flow

Each application area uses a small contract for its own task.

📚Teaching serviceClass schedules
🧾Fee serviceFee payments
🚌Transport serviceBus operations
 
 
 
ITeachingFocused contract
IFeeCollectionFocused contract
ISchoolBusOperationFocused contract

The animation traces each school service to its own contract, without a shared oversized interface.

Benefits for larger applications

  • Maintainability: A change to one contract affects fewer classes.
  • Testing: Tests can focus on the operations a class actually supports.
  • Flexibility: New roles can use existing contracts where they fit.
  • Clear ownership: Each interface describes a specific responsibility.
  • Safer evolution: Unrelated classes avoid unnecessary method changes.

Important trade-offs

  • Too many tiny interfaces can make navigation harder.
  • Interface names should describe meaningful business capabilities.
  • Do not split methods when they always belong to one clear operation.
  • Review interfaces when business responsibilities change.

Enterprise note: ISP improves code design. It does not automatically make an application distributed or faster. Network boundaries, deployment and data ownership need separate design decisions.

S6. Common Mistakes and Best Practices

Mistake 1: Keeping every method in one interface

Wrong: One interface describes teaching, fee collection and transport.

Why it fails: Every implementing class must provide unrelated methods.

Better: Split the contract around real business responsibilities.

Rule: Keep each interface focused on one useful capability.

Mistake 2: Creating an interface for every single method

Wrong: Create separate interfaces for tiny methods that always change together.

Why it fails: The design becomes harder to understand and navigate.

Better: Group methods that belong to one meaningful responsibility.

Rule: Split by client needs, not by method count.

Mistake 3: Using exceptions for unsupported operations

Wrong: Implement unrelated methods only to throw NotSupportedException.

Why it fails: The class advertises operations it cannot perform.

Better: Remove those methods from the class's contract.

Rule: A class should not need fake implementations to satisfy an interface.

Mistake 4: Giving interfaces vague names

Wrong: Use names such as ICommon or IManager for unrelated operations.

Why it fails: Developers cannot understand the contract's purpose quickly.

Better: Use names such as IFeeCollection and ITeaching.

Rule: Name interfaces after the capability they provide.

Mistake 5: Changing a shared interface without checking its clients

Wrong: Add a new method to a widely used interface without reviewing its consumers.

Why it fails: Existing implementations may need changes they do not need.

Better: Consider a separate interface for the new capability.

Rule: Review who depends on an interface before expanding it.

S7. How This Comes Up in Interviews

Question 1: What is the Interface Segregation Principle?

What the interviewer wants: They want to know whether you can design focused contracts.

Strong answer:

"The Interface Segregation Principle is the I in SOLID. It says that clients should not depend on methods they do not use. I prefer smaller interfaces based on business responsibilities. For example, in a school application, teachers need teaching operations. Accountants need fee operations. Bus drivers need transport operations. I would create separate interfaces for those capabilities."

Question 2: Can one class implement multiple interfaces?

Strong answer:

"Yes. A class can implement multiple interfaces when it supports those responsibilities. For example, a school coordinator may teach classes and collect fees. The class can implement both ITeaching and IFeeCollection. This keeps each contract focused while supporting the coordinator's actual work."

Question 3: How is ISP different from SRP?

Strong answer:

"SRP focuses on keeping a class responsible for one reason to change. ISP focuses on keeping interfaces small enough for their clients. They support each other, but they solve different design problems."

Question 4: Does ISP mean every interface must have one method?

Strong answer:

"No. ISP does not set a fixed method count. An interface can have several methods when they belong to one capability. The important point is that its clients should need the methods it exposes."

What should different experience levels explain?

  • Junior developer: Explain the principle and show a small interface split.
  • Senior developer: Explain how it reduces unwanted dependencies and change impact.
  • Technical architect: Explain how contracts align with business capabilities and application boundaries.

S8. Hands-On Practice

Beginner: School staff interfaces

Create interfaces for teachers, accountants and school bus drivers. Each class should implement only its required interface.

Expected result: No class should contain an unsupported operation.

Intermediate: Add a school coordinator

Create a coordinator that teaches classes and collects fees. Implement both relevant interfaces.

Hint: A class can implement more than one interface.

Advanced: Review a hospital application

A hospital system has one large interface with appointment booking, billing, pharmacy stock and ambulance dispatch methods.

Identify the clients that need each capability. Split the interface into focused contracts. Then explain how your design handles a new pharmacy role.

Expected result: Each class depends only on the capabilities it needs.

S9. Next Steps and Community

Previous article: SOLID - Liskov Substitution Principle.

Next article: SOLID - Dependency Inversion Principle.

Continue the SOLID series by learning how high-level code can depend on abstractions instead of concrete implementations.

Try the examples in a .NET console project. Share your interface designs and improvements with the Upgradix community. Help other developers understand the principle through practical examples.

Keep interfaces focused, and let each class depend only on what it needs.

Upgradix by Shrash Studio
Learn Technology. Build Your Future.
Quality Technology Education. Completely Free.

Comments & Ratings (0)

Log in or create a student account to comment and rate this article.

Be the first to comment on this article.