Grokking SOLID Design Principles
Ask Author
Back to course home

0% completed

Using Composition to Follow Liskov Substitution Principle (LSP)
Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

In the previous lesson, we explored how using inheritance can sometimes violate the Liskov Substitution Principle. Another way to follow LSP without falling into the pitfalls of inheritance is by using composition over inheritance. This method allows you to design systems that are more flexible and avoid situations where subclasses cannot properly fulfill the parent class's contract.

Example: Bird and Flying Behavior

Let’s use the example of birds. If we use inheritance, we might create a Bird class that defines general bird behaviors, like flying

.....

.....

.....

Like the course? Get enrolled and start learning!

Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible