Friday, 27 February 2026

Interface Segregation Principle - SOLID Principle

In this article we are going to see the Interface Segregation Principle, which is used to separate the interfaces with corresponding methods, because classes should not define the interface methods which is not used. 

For example we will have to interface IWork, which consists of two methods one is DoWork and another one is TakeFood. this interface is deriving in classes named HumanWorker and RobotWorker where RobotWorker doesnt need to use the method TakeFood so it should not implemented, please see the below example.

  internal interface IWork
    {
        void DoWork();

        void TakeFood();
    }

    internal class HumanWorker : IWork
    {
        public void DoWork()
        {
            Console.WriteLine("Working");
        }

        public void TakeFood()
        {
            Console.WriteLine("Eating Food");
        }
    }

    internal class RobotWorker : IWork
    {
        public void DoWork()
        {
            Console.WriteLine("Working");
        }

        public void TakeFood()
        {
            throw new NotImplementedException("N/A");
        }
    }

After implementing the ISP, the above interface is segregated in to two interfaces and apply the interface which is really required for that classes.

    internal interface IWork
    {
        void DoWork();
    }

    internal interface ITakeFood
    {
        void TakeFood();
    }

    internal class HumanWorker : IWork, ITakeFood
    {
        public void DoWork()
        {
            Console.WriteLine("Working");
        }

        public void TakeFood()
        {
            Console.WriteLine("Eating Food");
        }
    }

    internal class RobotWorker : IWork
    {
        public void DoWork()
        {
            Console.WriteLine("Working");
        }
    }

From this article we can learn interface segregation principle ISP in SOLID Principle.