composition-over-inheritance

Replace inheritance hierarchies with interface-based composition in class designs.

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

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Inheritance creates tight coupling and rigid hierarchies. This Skill promotes composition to build flexible, reusable components that can be mixed and matched.

Core Features & Use Cases

  • Encourages defining capabilities as interfaces or small composable units and injecting them into classes instead of extending concrete parents.
  • Helps restructure class relationships by extracting shared behavior into composable components.
  • Use Case: convert an is-a inheritance hierarchy into a set of behaviors that can be combined to form diverse objects.

Quick Start

Define small, reusable capabilities as interfaces or simple objects; compose them into concrete classes by wiring the behaviors; and gradually refactor existing inheritance-heavy code to rely on composition.

Frequently Asked Questions about composition-over-inheritance

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

FAQPage Schema
How do I refactor an inheritance hierarchy to use composition?

To refactor inheritance to composition, extract shared behaviors into small composable units or interfaces, then inject and wire these capabilities into concrete classes instead of extending concrete parents, reducing tight coupling and rigid hierarchies.

Why does inheritance create tight coupling in object-oriented design?

Inheritance creates tight coupling because subclasses deeply depend on the implementation details of their concrete parents, forming rigid hierarchies. Composition solves this by enabling runtime behavior injection and combining mixable, interface-based capabilities.

What is the best way to mix and match object behaviors dynamically?

The best way to mix and match object behaviors is using composition over inheritance. Define capabilities as interfaces or small composable objects, and wire them into classes to enable runtime behavior injection and flexible component reuse.

When do I need to replace is-a class relationships with composable components?

You need to replace is-a class relationships when facing rigid hierarchies or needing mixable behaviors. Converting is-a inheritance into a set of composable capabilities allows you to combine behaviors dynamically and avoid deep inheritance trees.

Can I inject runtime behaviors without extending concrete parent classes?

Yes, you can inject runtime behaviors by defining capabilities as small composable units or interfaces and wiring them into your classes. This composition approach avoids extending concrete parents and enables flexible behavior injection.

What are the limitations of using composition over inheritance for refactoring?

Using composition over inheritance shifts complexity from class hierarchies to object wiring. While it safely avoids deep inheritance and enables runtime behavior injection, it requires carefully designing interface-based capabilities to ensure components mix cohesively without tight coupling.