Tell about the SOLID principles.
sobes.tech AI
Answer from AI
SOLID principles are a set of five fundamental principles of object-oriented programming aimed at creating flexible, extendable, and maintainable code:
-
S — Single Responsibility Principle Each class should have only one reason to change, meaning it should be responsible for a single specific task.
-
O — Open/Closed Principle Software entities should be open for extension but closed for modification.
-
L — Liskov Substitution Principle Subclass objects should be interchangeable with superclass objects without affecting the correctness of the program.
-
I — Interface Segregation Principle Clients should not depend on interfaces they do not use. It's better to have several specific interfaces than a single universal one.
-
D — Dependency Inversion Principle High-level modules should not depend on low-level modules. Both should depend on abstractions.
Example in Java for the Single Responsibility Principle:
class Report {
String content;
// Responsible only for report data
}
class ReportPrinter {
void print(Report report) {
// Responsible only for outputting the report
System.out.println(report.content);
}
}
This separation of responsibilities makes the code easier to maintain and extend.