A topic in the Open Knowledge Graph — a free, open map of 15,495 topics and the order to learn them in.

Inheritance and Subclasses

College Depth 90 in the knowledge graph ☐ I know this ☆ Set as goal
2topics build on this
338prerequisites beneath it
See this on the map →
Introduction to ClassesMethods and Attributes+1 more→→Abstract Classes and InterfacesMethod Overriding and Polymorphism
inheritance subclasses superclass OOP object-oriented code-reuse

Core Idea

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.

How It's Best Learned

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.

Common Misconceptions

Explainer

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.

Practice Questions 4 questions

Prerequisite Chain

Understanding Zero → The Number Zero → Counting to Five → Counting to 10 → Counting to 20 → Counting a Set of Objects Up to 20 → Cardinality: The Last Number Counted → Matching Numerals to Quantities → Subitizing Small Quantities → Combining Small Groups Within 5 → Number Bonds Within 5 → Number Bonds to 10 → Tens and Ones Place Value → Reading and Writing Three-Digit Numbers → Comparing Three-Digit Numbers → Comparing and Ordering Three-Digit Quantities → Comparing and Ordering Whole Numbers → Comparing Numbers 1 to 20 → Ordering Numbers to 20 → Number Line 0 to 20 → Addition Within 20 → Doubles and Near Doubles → Doubles Facts Within 10 → Near Doubles Facts Within 20 → Mental Math Strategies for Addition → Mental Math: Adding and Subtracting Tens → Addition Within 100 → Repeated Addition as Multiplication → Multiplication as Equal Groups → Multiplication: Arrays → Basic Multiplication Facts (0s, 1s, 2s, 5s, 10s) → Multiplication Facts Within 100 → Division as Equal Sharing → Division as Grouping (Measurement Division) → Division: Grouping (Repeated Subtraction) Model → Division: Fair Sharing Model → Division as Equal Sharing → Division as Grouping → Basic Division Facts → Division Facts Within 100 → Multiplication and Division Fact Families → Relationship Between Multiplication and Division → Division Facts as Inverse of Multiplication → Remainders and Quotients in Division → Division Word Problems → Multi-Step Word Problems → Solving Multi-Step Word Problems → Multiplication Word Problems → Division Word Problems → Introduction to Long Division → Factors and Multiples → Prime and Composite Numbers → Equivalent Fractions → Relating Fractions and Decimals → Decimal Place Value → Integers and the Number Line → Opposites and Additive Inverses → Absolute Value → Adding Integers → Subtracting Integers → Multiplying Integers → Introduction to Exponents → Order of Operations → Integer Order of Operations → Variable Expressions → The Distributive Property → Variables and Expressions Review → Introduction to Polynomials → Adding and Subtracting Polynomials → Multiplying Polynomials → Factorial → Permutations → Combinations → Counting Principles: Addition and Multiplication Rules → Introduction to Graph Theory → Propositional Logic Foundations → Logical Equivalences → Boolean Algebra → Boolean Type and Truth Values → Comparison Operators and Boolean Tests → Logical Operators and Boolean Algebra → Conditional Statements → Defining and Calling Functions → Functions: Decomposing Problems → Function Parameters and Argument Passing → Return Values → Variable Scope → Introduction to Classes → Objects and Instances → Methods and Attributes → Inheritance and Subclasses

Longest path: 91 steps · 338 total prerequisite topics

Prerequisites (3)

Leads To (2)