Method overriding is the mechanism by which a subclass provides its own implementation of a method that it inherits from a superclass, keeping the same name, parameter list and compatible return type. In object-oriented programming, overriding is the foundation of runtime polymorphism: when code calls the method through a reference to the base type, the version that executes is chosen by the actual class of the object, not by the declared type of the variable.
Overriding lets a class hierarchy share a common contract while each subclass supplies behavior that fits its own data. A Shape class may declare area(), and Circle and Rectangle each override it with the correct formula, so callers can compute areas without knowing which concrete shape they hold.
How method overriding is resolved at runtime
Overridden methods are selected through dynamic dispatch, also called late binding. The compiler checks that the method exists on the declared type, but the decision about which body to run is postponed until execution, when the runtime inspects the object. Most statically typed languages implement this with a virtual method table (vtable): each class holds a table of function pointers, and a subclass that overrides a method replaces the corresponding entry.
A call through a base reference therefore costs one extra indirection, a lookup in the vtable followed by an indirect jump. Modern just-in-time compilers in the Java Virtual Machine and .NET often remove this cost through devirtualization and inlining when they can prove which class is involved. In dynamically typed languages such as Python and Ruby, the runtime searches the class and its ancestors along the method resolution order instead of using a fixed table.
The table below shows how the main resolution concepts relate to one another.
| Concept | Role in overriding | Decided at |
|---|---|---|
| Declared type | Determines which methods may be called | Compile time |
| Runtime type | Determines which implementation runs | Run time |
| Vtable | Maps method slots to class implementations | Class loading |
| Method resolution order | Ancestor search sequence in dynamic languages | Class creation |
| Super call | Invokes the parent implementation explicitly | Compile time |
Rules a valid override must follow
A method only overrides another when its signature matches the inherited one; otherwise the compiler treats it as a new, unrelated method. The rules follow from the Liskov substitution principle: any code written against the superclass must keep working when it receives a subclass instance, so an override may not demand more or promise less than the original.
- Same name and parameter types: changing a parameter type creates an overload, not an override.
- Covariant return types: Java, C# 9 and C++ allow the override to return a more specific subtype.
- Access cannot be narrowed: a public method cannot become private or protected in a subclass.
- Checked exceptions cannot widen: in Java, an override may throw fewer or narrower checked exceptions, never broader ones.
- Final, sealed and static members are excluded: they cannot be overridden, although static methods can be hidden.
Languages differ in how explicit the programmer must be. The next table compares the keywords and defaults in widely used languages.
| Language | Methods virtual by default | Override marker | Block further overriding |
|---|---|---|---|
| Java | Yes | @Override annotation, optional | final |
| C# | No, requires virtual | override keyword, required | sealed |
| C++ | No, requires virtual | override specifier, optional | final |
| Kotlin | No, requires open | override keyword, required | final override |
| Python | Yes | None; @override decorator from 3.12 | typing.final, advisory |
| Swift | Yes for classes | override keyword, required | final |
Using the explicit override marker wherever it is optional is a widely recommended practice. It turns a silent mistake, such as a misspelled name or a wrong parameter type, into a compile-time error instead of a method that is never called.
Method overriding compared with method overloading and hiding
Overriding is often confused with method overloading, although the two solve different problems. Overloading defines several methods with the same name but different parameter lists in one class, and the compiler chooses among them from the argument types. Overriding redefines one inherited method with the same signature, and the runtime chooses by object type.
| Aspect | Method overriding | Method overloading | Method hiding |
|---|---|---|---|
| Relationship required | Inheritance | None, same class is enough | Inheritance |
| Signature | Identical | Different parameters | Identical |
| Binding | Dynamic, run time | Static, compile time | Static, compile time |
| Applies to | Instance methods | Any method | Static methods, non-virtual members |
| Polymorphism type | Subtype polymorphism | Ad hoc polymorphism | None |
Method hiding is the subtle third case. In Java, a static method with the same signature in a subclass hides the parent version, and in C# a non-virtual method redefined with the new modifier does the same. The method chosen then depends on the declared type of the reference, which surprises developers who expect overriding behavior.
Calling the parent implementation
An override does not have to discard the inherited logic. Through super in Java, Python and Kotlin, base in C#, or a qualified name such as Parent::method() in C++, the subclass can extend rather than replace the original behavior, for example by validating input and then delegating to the parent.
Frameworks rely heavily on this pattern. The template method pattern defines an algorithm in a base class and lets subclasses override selected hook methods, while lifecycle callbacks in user interface toolkits frequently require the override to call the parent implementation first. Forgetting that call is a classic source of resource leaks and broken initialization.
Pitfalls of method overriding
Overriding is powerful, but it couples subclasses tightly to the internal behavior of their parents. This is the fragile base class problem, and it is one reason many design guides recommend composition over inheritance for code that is not designed for extension. The table lists frequent symptoms and their fixes.
| Symptom | Likely cause | Remedy |
|---|---|---|
| Subclass method never runs | Signature mismatch created an overload | Add an explicit override marker |
| Half-initialized object | Overridable method called from constructor | Call only final or private methods in constructors |
| Parent state not updated | Missing super call | Delegate to the parent implementation |
| Hash-based collections misbehave | equals overridden without hashCode | Override both consistently |
| Behavior breaks after library upgrade | Fragile base class dependency | Seal classes or prefer composition |
Calling an overridable method from a constructor deserves special attention. In Java and C#, the subclass override runs before the subclass constructor has initialized its fields, so it may observe null or default values. In C++, the call is resolved to the class currently being constructed, which avoids that error but surprises programmers who expected polymorphic behavior.
A plain-language summary
Method overriding means that a more specific kind of thing can do a shared action in its own way. Imagine a company rule that says every employee must submit a monthly report. A salesperson reports deals, and an engineer reports finished features. The instruction is the same, but each role carries it out differently.
In code, the general class sets the rule, and each specialized class rewrites how that rule is followed. When the program asks any object to act, the object itself decides which version runs, based on what it really is.
An example from practice
A payment platform supports card payments, bank transfers and digital wallets. Its developers define an abstract class PaymentMethod with the methods authorize() and refund(). Each payment type is a subclass that overrides both methods with the calls required by its own provider, including different timeouts and error codes.
The checkout service holds only a list of PaymentMethod references and calls authorize() on whichever one the customer selected. When the team adds a fourth option, a buy-now-pay-later provider, they write one new subclass with two overrides and change no checkout code at all. The release touches about 300 lines instead of several thousand, and regression testing focuses on the new class, which shortens the delivery cycle from weeks to days.