A topic in the Open Knowledge Graph — a free, open map of 15,495 topics and the order to learn them in.
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.
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.
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.