SOLID & OOP Principles (50 Qs)•Part 1: Core SOLID Principles & Fundamentals (1 - 10)
What is the Liskov Substitution Principle (LSP) and what is the classic Rectangle-Square violation?
junior Levelsolid-principlesCore SOLID Principles
In Simple Words: Subclasses must adhere to the behavioral contract of their parent class:
- The Rectangle-Square Violation:
Squareinherits fromRectangle.- In
Square, settingwidthautomatically setsheightto maintain a square. - A function expecting a
Rectanglesetswidth = 5andheight = 10and expects area to be 50. - Passing
Squareproduces area 100, silently violating mathematical assumptions and breaking caller code!
- Rule: If a subtype cannot fulfill all invariants of the parent, use composition instead of inheritance.