Liskov Substitution Principle (LSP)

Hands-on practice for this lecture. Work through the exercises and quizzes to reinforce what you've learned.

1

Exercise 1 of 1

LSP: The Substitution Test

Avoid the inheritance trap. Verify if your child classes can truly substitute for their parents, or if you're building a system destined to crash.

"If it looks like a Duck and quacks like a Duck, but needs batteries, you probably have the wrong abstraction."

Select a bird to test the "makeItFly(Bird b)" method.

🦆 Liskov Substitution Principle: Child classes should be replaceable for their parent classes without causing errors. If a child "breaks" a parent method (e.g. by throwing an exception), your inheritance is logically flawed.

Hands-on Labs

Practical exercises to master the concepts.

Practical Lab
LSP

LSP Violation Check

Create a Bird parent and a Ostrich child. Explain why Ostrich overriding fly() with an error violates LSP.

Try implementing this on your own first!
Practice: Liskov Substitution Principle (LSP) — Interactive Exercises | Durgesh Rai