From the course: Advanced Design Patterns: Design Principles

Unlock this course with a free trial

Join today to access over 22,400 courses taught by industry experts.

Liskov's substitution principle

Liskov's substitution principle

- [Instructor] With inheritance we drive classes that implement abstract methods and their base classes. But if we aren't careful, we can actually easily create problematic hierarchies. For instance, we might end up with a hierarchy that doesn't adhere well to our open-close principle. But how? Well let's break this down a little bit to understand it better. Let's say we have a class TypeA, and a method called doSomething that does something with the TypeA. So let's say we instantiate a new TypeA object, and then we pass it to our doSomething method and everything works as we'd expect. We'd also expect that we could sub class TypeA with a new class TypeB, and then use that with our doSomething method. We do that all the time and things usually work out fine. But we can end up with situations where when substituting our drive class, things actually do break in a bad way. That is, we can end up where we drive the sub…

Contents