Inheritance
Inheritance is an object-oriented programming mechanism that allows a class to acquire the properties and methods of another class, letting a subclass reuse and extend the behavior of a parent (base) class.
Definition
Inheritance is an object-oriented programming mechanism that allows a class to acquire the properties and methods of another class, letting a subclass reuse and extend the behavior of a parent (base) class.
Overview
Inheritance lets developers model 'is-a' relationships between classes: a `Dog` class can inherit from an `Animal` class because a dog is a kind of animal, automatically gaining `Animal`'s fields and methods while adding or overriding behavior specific to dogs. This is one of the foundational ideas of object-oriented programming and, alongside polymorphism, is what enables a subclass to be used anywhere its parent class is expected. Most mainstream languages support single inheritance, where a class can extend only one direct parent, though a class can still inherit indirectly through a chain of ancestors. Some languages, like C++, support multiple inheritance directly, while others, like Java and TypeScript, restrict classes to single inheritance but allow implementing multiple interfaces instead, sidestepping the ambiguity problems (like the 'diamond problem') that multiple class inheritance can introduce. Inheritance is powerful for code reuse, but overusing it — building deep hierarchies of classes that inherit from classes that inherit from other classes — is a well-known source of fragility, since a change to a base class can ripple unpredictably through every descendant. This has led to the widely cited principle of favoring composition over inheritance: instead of a subclass inheriting behavior, an object can be composed of smaller, focused objects that each provide one piece of functionality, which tends to be more flexible and easier to change safely. Inheritance also interacts with encapsulation: a well-designed base class exposes a clear protected or public interface for subclasses to extend, while keeping its internal implementation details hidden, so subclasses build on stable behavior rather than fragile internals.
Key Concepts
- Lets a subclass acquire fields and methods from a parent class
- Models 'is-a' relationships between related classes
- Most languages support single inheritance of classes, plus multiple interfaces
- Subclasses can override or extend inherited behavior
- Deep inheritance chains can create fragile, hard-to-change code
- 'Composition over inheritance' is a common alternative design principle
- Works closely with polymorphism to enable flexible, extensible code
Use Cases
Frequently Asked Questions
From the Blog
Python Inheritance and Polymorphism Explained
Inheritance lets a class reuse another's code; polymorphism lets different objects share one interface. Learn both pillars of Python OOP with clear examples.
Read More ProgrammingJavaScript prototypes and inheritance explained
JavaScript has one inheritance mechanism — a lookup chain between objects — and class syntax is a readable surface over it. Knowing the chain is what turns shared mutable state, instanceof results and prototype pollution from mysteries into predictable consequences of how property lookup works.
Read More ProgrammingObject-Oriented Programming in Python: A Practical Guide
OOP is how Python codebases stay organised as they grow. This guide explains classes, inheritance, encapsulation, and polymorphism with real examples — and tells you honestly when to use OOP and when plain functions are the better choice.
Read More ProgrammingObject-Oriented Programming: The Four Pillars
Object-oriented programming organizes code around objects using four pillars: encapsulation, abstraction, inheritance, and polymorphism. Learn each clearly.
Read More