A topic in the Open Knowledge Graph — a free, open map of 15,495 topics and the order to learn them in.
Inheritance lets a new class (the subclass) be defined as an extension of an existing class (the superclass). The subclass automatically gets the superclass's attributes and methods, and it can add new ones of its own. Inheritance models an "is-a" relationship: a SavingsAccount is a BankAccount, so it should do everything a BankAccount does and possibly more. Shared code then lives in one place instead of being copied into every related class.
Start from two classes that share obvious duplicated code, such as Car and Truck that both track speed and fuel, and refactor the shared part into a Vehicle superclass. Trace object creation step by step to show that the subclass constructor must make sure the superclass part of the object is set up, using `super()`. Contrast "is-a" with "has-a" (composition) using concrete examples so students do not use inheritance for every relationship.
You already know how to write a class with attributes and methods. Now suppose you write a `BankAccount` class with a balance, a `deposit` method and a `withdraw` method. Later you need a `SavingsAccount` that does all of that and also earns interest. Copying the whole class and adding one method would work, but then every bug fix must be made twice. Inheritance solves this.
In Python you write `class SavingsAccount(BankAccount):`. In Java you write `class SavingsAccount extends BankAccount`. `BankAccount` is the superclass (also called the parent or base class) and `SavingsAccount` is the subclass (child or derived class). A `SavingsAccount` object can call `deposit` and `withdraw` even though the subclass never defines them, because it inherits them. The subclass adds only what is new, such as an `interest_rate` attribute and an `add_interest` method.
Construction needs care. A subclass object contains a superclass part, and that part must be set up too. In Python, the subclass `__init__` usually begins with `super().__init__(owner, balance)` and then sets its own attributes. In Java, the subclass constructor calls `super(owner, balance)`. Before Java 25 that call had to be the first statement; Java 25 allows some statements before it, but they cannot use the object being built. If a Java subclass constructor has no explicit `super(...)` call, the compiler inserts a call to the superclass's no-argument constructor.
The test for using inheritance is the "is-a" rule. A savings account is a bank account, so inheritance fits. A car has an engine, but a car is not an engine, so the engine should be an attribute of the car. Storing one object inside another is called composition, and it models "has-a" relationships.
Inheritance can be chained: `Animal` to `Mammal` to `Dog`. Each level inherits everything above it. In Java every class ultimately extends `Object`, and in Python 3 every class ultimately inherits from `object`. That is why even a brand-new class already has some methods, such as `toString()` in Java or `__str__` in Python.
Inheritance is also the base for the next idea. A subclass can replace an inherited method with its own version. That is called overriding, and it lets one line of code behave differently depending on the type of object it runs on.