Sobes.tech
Junior — Middle

What is the meaning of the letter 'I' in the SOLID principles, and what is its purpose in object-oriented programming?

sobes.tech AI

Answer from AI

The letter 'I' in SOLID principles stands for the Interface Segregation Principle.

The essence of this principle is that clients should not depend on interfaces they do not use. It is better to create several specialized interfaces than one general and bulky interface.

Purpose in OOP:

  • Increases flexibility and modularity of the code.
  • Simplifies maintenance and extension.
  • Avoids unnecessary dependencies and coupling.

Example in C#:

interface IPrinter {
    void Print();
}

interface IScanner {
    void Scan();
}

class MultiFunctionDevice : IPrinter, IScanner {
    public void Print() { /* implementation of printing */ }
    public void Scan() { /* implementation of scanning */ }
}

class SimplePrinter : IPrinter {
    public void Print() { /* only printing */ }
}

Here, interfaces are separated by functionality, and a class can implement only the interfaces it needs.