What is the core idea behind the Interface Segregation Principle (ISP)?
Code should be open for extension but closed for modification.
Classes should have only one responsibility.
Subclasses should be substitutable for their base classes.
Clients should not be forced to depend on methods they don't use.
In the context of DIP, what are abstractions typically represented by?
Database connections
Concrete classes
Interfaces or abstract classes
User interface components
Which of these is NOT a valid approach to refactor a class violating SRP?
Use design patterns like Strategy or Template Method to separate concerns.
Combine all the responsibilities into a single method for better cohesion.
Extract separate functionalities into new classes.
Delegate responsibilities to other existing classes.
The Liskov Substitution Principle primarily deals with the relationship between:
Subclasses and their superclasses
Classes and their instances
All of the above
Interfaces and their implementations
In the context of LSP, what is meant by 'substitutability'?
The ability to change the behavior of a superclass by modifying its subclass.
The practice of always using abstract classes instead of concrete classes.
The idea that all methods in a subclass should be static.
The capability to use a subclass object wherever a superclass object is expected without causing issues.
How can you identify multiple responsibilities within a class?
By counting the number of lines of code in the class.
By checking if the class name is too long.
By looking for different reasons to change the class in the future.
By observing how many other classes it interacts with.
In the context of the Open/Closed Principle, what does 'open for extension' mean?
A class's behavior should be modifiable through inheritance or polymorphism.
Code should be heavily commented to explain every detail.
The source code of a class should always be accessible for any developer to modify.
All classes should be loosely coupled and easily replaceable.
Imagine a class named 'Employee' that handles both employee data (like name, ID) and database operations. What SOLID principle does this violate?
Interface Segregation Principle
Single Responsibility Principle
Open/Closed Principle
Liskov Substitution Principle
How does the Interface Segregation Principle contribute to loose coupling in software design?
By reducing the need for unit testing.
By promoting the use of concrete classes instead of interfaces.
By minimizing dependencies between classes to only what is absolutely necessary.
By encouraging the use of global variables for communication between classes.
Imagine an interface IWorker with methods work() and takeBreak(). You have two classes, Robot and Human, both implementing IWorker. How would you refactor this to better align with the ISP?
IWorker
work()
takeBreak()
Robot
Human
Make takeBreak() an abstract method within IWorker so only Human has to implement it.
Have Robot implement IWorker, but leave Human without an interface.
Keep the IWorker interface as is, as both classes can perform both actions.
Create two separate interfaces: IWorkable with work(), and IRest with takeBreak(). Implement them accordingly.
IWorkable
IRest