How does the Interface Segregation Principle contribute to loose coupling in software design?
By reducing the need for unit testing.
By minimizing dependencies between classes to only what is absolutely necessary.
By encouraging the use of global variables for communication between classes.
By promoting the use of concrete classes instead of interfaces.
Imagine a class named 'Employee' that handles both employee data (like name, ID) and database operations. What SOLID principle does this violate?
Open/Closed Principle
Liskov Substitution Principle
Interface Segregation Principle
Single Responsibility Principle
A class EmailService implements an interface ICommunication with methods sendEmail(), sendSMS(), and makeCall(). Only the email functionality is relevant to EmailService. How can this design be improved according to ISP?
EmailService
ICommunication
sendEmail()
sendSMS()
makeCall()
Move all methods (sendEmail(), sendSMS(), makeCall()) to the EmailService class.
Keep the current design, as it provides a single point of access for all communication methods.
Throw an exception in EmailService for the unused methods: sendSMS() and makeCall().
Create separate interfaces: IEmailService, ISMSService, and ICallService, and have EmailService implement only IEmailService.
IEmailService
ISMSService
ICallService
What is the core idea behind the Interface Segregation Principle (ISP)?
Subclasses should be substitutable for their base classes.
Code should be open for extension but closed for modification.
Clients should not be forced to depend on methods they don't use.
Classes should have only one responsibility.
Consider a base class 'Bird' with a method 'fly()'. You create a subclass 'Penguin'. What would be the most LSP-compliant approach in this scenario?
Have 'Penguin' inherit 'fly()' but leave it empty, as penguins don't need to fly.
Remove the 'fly()' method from the 'Bird' class entirely.
Have 'Penguin' inherit 'fly()' and throw an exception, as penguins cannot fly.
Rethink the inheritance hierarchy. 'Bird' might not be the right superclass for 'Penguin' if flying is a core characteristic.
What is the core idea behind the Single Responsibility Principle (SRP)?
A class should interact with as few other classes as possible.
A class should be as small as possible.
A class should have a single, well-defined responsibility.
A class should have only one method.
How can you identify multiple responsibilities within a class?
By checking if the class name is too long.
By looking for different reasons to change the class in the future.
By counting the number of lines of code in the class.
By observing how many other classes it interacts with.
SOLID principles primarily aim to improve which aspect of software development?
Execution Speed
Memory Optimization
Code Maintainability
Algorithm Efficiency
Which of the following is a direct benefit of applying SOLID principles?
Faster initial development speed
Reduced need for documentation
Improved code reusability and extensibility
Guaranteed bug-free software
In the context of LSP, what is meant by 'substitutability'?
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.
The practice of always using abstract classes instead of concrete classes.
The ability to change the behavior of a superclass by modifying its subclass.