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

Method Overriding and Polymorphism

College Depth 91 in the knowledge graph ☐ I know this ☆ Set as goal
1topic build on this
339prerequisites beneath it
See this on the map →
Inheritance and SubclassesDefining and Calling Functions→→Abstract Classes and Interfaces
polymorphism overriding dynamic-dispatch OOP object-oriented super

Core Idea

A subclass can override an inherited method by defining a method with the same name and parameters. When the method is called, the version that runs is chosen by the actual type of the object at run time, not by the type of the variable that holds it. This run-time choice is called dynamic dispatch, and it is what makes subtype polymorphism work: one call such as `shape.area()` gives the right answer for a circle, a square or any future shape class.

How It's Best Learned

Build a small hierarchy (Shape, Circle, Rectangle) where each subclass overrides `area()`, then loop over a mixed list and call `area()` on each item. Trace Java examples that separate the declared type from the actual type, such as `Shape s = new Circle(2);`, and ask which method runs and which calls the compiler accepts. Compare overriding with overloading side by side so the two terms do not blur.

Common Misconceptions

Explainer

From inheritance you know a subclass gets every method of its superclass. Sometimes the inherited behavior is wrong for the subclass. Every `Shape` might have an `area()` method, but a circle and a rectangle compute area differently. The subclass fixes this by overriding: it defines a method with the same name and the same parameters. In Java you can mark it with `@Override`, which asks the compiler to confirm that a matching superclass method really exists.

Overriding becomes powerful through dynamic dispatch. In Java you may write `Shape s = new Circle(2.0);`. The variable has declared type `Shape`, but the object is a `Circle`. When you call `s.area()`, Java looks at the object, finds that it is a `Circle`, and runs `Circle`'s `area()`. Python works the same way: the method is looked up on the object's class at the moment of the call.

Keep two questions apart. First, will the call compile? In Java, the compiler checks the declared type, so `s.area()` compiles only if `Shape` declares `area()`. Second, which version runs? That is decided at run time by the object's actual class. Students who mix these up expect `s.radius()` to compile because the object is a circle, or expect `Shape`'s `area()` to run because the variable says `Shape`.

This is subtype polymorphism, from Greek words meaning "many forms". A single line, `total += shape.area()`, gives the correct result for circles, squares and triangles. Better still, a programmer can add a `Hexagon` class next year and the same loop handles it without any edits. That is why large programs are often written against a superclass rather than against each concrete class.

An overriding method can still use the superclass version. In Java, `super.describe()` calls the parent's method. In Python, `super().describe()` does the same. A common pattern is to call the parent version and then add the subclass's extra detail.

Do not confuse overriding with overloading. Overloading means one class has several methods with the same name but different parameter lists, such as `print(int)` and `print(String)`. The compiler picks among overloads from the argument types at compile time. Overriding is chosen at run time from the object's type.

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 → Method Overriding and Polymorphism

Longest path: 92 steps · 339 total prerequisite topics

Prerequisites (2)

Leads To (1)