How does inheritance relate to the Liskov Substitution Principle?
Inheritance is a prerequisite for applying LSP; it guides the correct implementation of inheritance.
LSP only applies when using multiple interfaces, not inheritance.
LSP dictates that all classes must inherit from a single base class.
Inheritance has no direct relationship with LSP.
What is the core idea behind the Interface Segregation Principle (ISP)?
Classes should have only one responsibility.
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.
In the context of the Open/Closed Principle, what does 'open for extension' mean?
The source code of a class should always be accessible for any developer to modify.
A class's behavior should be modifiable through inheritance or polymorphism.
All classes should be loosely coupled and easily replaceable.
Code should be heavily commented to explain every detail.
Which scenario best exemplifies the Dependency Inversion Principle?
A 'DataProcessor' class depends on an 'IDataSource' interface, not a specific database implementation.
A 'Logger' class writes logs directly to a file.
A 'LightSwitch' class is directly dependent on a 'LightBulb' class.
A 'Car' class directly instantiates and uses an 'Engine' class.
Which of these is NOT a benefit commonly associated with applying the Dependency Inversion Principle?
Increased code complexity
Improved testability
Enhanced code reusability
Reduced code coupling
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()
Throw an exception in EmailService for the unused methods: sendSMS() and 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.
Create separate interfaces: IEmailService, ISMSService, and ICallService, and have EmailService implement only IEmailService.
IEmailService
ISMSService
ICallService
The Liskov Substitution Principle primarily deals with the relationship between:
Classes and their instances
Interfaces and their implementations
Subclasses and their superclasses
All of the above
How can you identify multiple responsibilities within a class?
By counting the number of lines of code in the class.
By looking for different reasons to change the class in the future.
By checking if the class name is too long.
By observing how many other classes it interacts with.
How does using interfaces contribute to the Open/Closed Principle?
Interfaces force tight coupling between classes.
Interfaces allow for direct modification of existing class code.
Interfaces make code execution slower.
Interfaces define a contract that can be implemented by multiple classes, enabling flexibility.
How does the Open/Closed Principle contribute to creating maintainable code?
It reduces the need for code documentation.
It allows for easier addition of new features without impacting existing ones.
It makes the codebase smaller.
It eliminates the need for testing.