liskov-substitution-principle

Enforce Liskov Substitution Principle in class hierarchies during design.

13|2|Updated Jan 21, 2026
One-click install
npx skills add https://github.com/yanko-belov/code-craft --skill liskov-substitution-principle
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: liskov-substitution-principle
Source: https://github.com/yanko-belov/code-craft/tree/main/skills/liskov-substitution
Command: npx skills add https://github.com/yanko-belov/code-craft --skill liskov-substitution-principle

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill helps engineers ensure that subclasses can replace parent types without altering program behavior, preventing Liskov Substitution Principle violations.

Core Features & Use Cases

  • Contract-aware inheritance: Guides when to use inheritance or switch to interfaces and composition.
  • Override discipline: Flags throwing or no-op overrides that break substitutability.
  • Design best practices: Recommends separating capabilities via interfaces to maintain correct behavior across hierarchies.
  • Use Case: When refactoring a class hierarchy, apply this skill to validate that all subclasses uphold the parent's contracts and invariants.

Quick Start

Review your current class hierarchy to ensure all overrides preserve contracts and avoid throwing exceptions.

Frequently Asked Questions about liskov-substitution-principle

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
What is the Liskov Substitution Principle in class hierarchies?

The Liskov Substitution Principle ensures subclasses can replace parent types without altering program behavior. It requires overrides to preserve contracts and invariants, preventing broken substitutability across component hierarchies.

How do I stop subclasses from throwing exceptions in method overrides?

To stop subclasses from throwing exceptions in method overrides, enforce override discipline by flagging throwing or no-op implementations. This ensures subclasses uphold parent contracts and maintains correct substitutability.

When should I use composition over inheritance to prevent LSP violations?

Use composition over inheritance when subclassing risks breaking parent contracts. Separating capabilities via interfaces and using composition prevents LSP violations that arise from forcing incompatible behaviors into a rigid hierarchy.

How do I validate that all subclasses uphold parent contracts during refactoring?

Validate subclass contracts during refactoring by reviewing the class hierarchy to ensure all overrides preserve invariants. This checks for no-op implementations and throwing overrides to guarantee correct behavior across components.

Why does my subclass override break the program's expected behavior?

Your subclass override breaks expected behavior because it violates the parent's contract. No-op implementations or throwing new exceptions disrupt substitutability, meaning the subclass can no longer replace the parent type cleanly.

Can I separate capabilities via interfaces to maintain substitutability?

Yes, you can separate capabilities via interfaces to maintain substitutability. This design practice prevents LSP violations by allowing components to adopt specific behaviors without forcing incompatible method overrides in a class hierarchy.