Learning Liskov Substitution Principle

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

The Liskov Substitution Principle

The Liskov Substitution Principle states that an object may be replaced by a sub-object without breaking the program. This means that everything should work the same even if the sub-object is used instead of the base object.

Exploring Scenarios for the Liskov Substitution Principle

For example, if a car rental company guarantees that its cars have at least two seats, four tires, and use gasoline, then any brand or model of car rented from them should meet these criteria. If a rented car doesn’t meet these guarantees, it’s a violation of the principle.

Importance of the Liskov Substitution Principle

Violating this principle can result in poorly structured code that requires additional checks and special handling for objects of the same type. It can also be a sign that classes have been generalized too early or that their relationships have been established incorrectly. A new subclass may not fit every behavior it inherits.

See forum comments
Download course materials from Github
Previous: Introduction Next: Implementing Liskov Substitution Principle