Method Overriding in JavaScript

Method overriding lets a subclass define a method with the same name as an inherited method, replacing that behavior for instances of the subclass. In JavaScript, this commonly appears with classes and prototype inheritance.

A subclass inherits methods from its parent through the prototype chain. When the subclass defines a method with the same name, property lookup finds the subclass method first, so calls on subclass instances use the new implementation.

Overriding is resolved at runtime through normal JavaScript property lookup. This supports polymorphism: code can call the same method on different objects while each object provides its own implementation.

The `super` keyword lets an overriding method invoke the parent implementation. This is useful when the subclass wants to extend rather than completely replace the inherited behavior.

JavaScript does not require an explicit `override` keyword. A method can override an inherited method simply by using the same property name in the subclass prototype. This differs from some statically typed languages that provide compiler checks for intentional overrides.

Key points

Interview questions

What is method overriding in JavaScript?

Method overriding occurs when a subclass defines a method with the same name as a method inherited from its parent. Calls on instances of the subclass resolve to the subclass implementation. The parent method remains available through the parent prototype. This allows different subclasses to provide different behavior behind the same method interface.

How does `super` work when overriding a method?

Inside a class method, `super.method()` invokes the corresponding method from the parent class. It is commonly used when an override needs to preserve some parent behavior and add or modify it. The subclass method still controls the overall call. This avoids duplicating the parent's implementation.

Why is method overriding important for polymorphism?

Polymorphism allows code to work with objects through a common interface while their implementations differ. A parent type or shared abstraction can expose a method such as `render()`, while subclasses provide their own implementations. The caller can invoke `render()` without knowing which subclass implementation will execute. In JavaScript, runtime property lookup determines the method that is called.

How does method overriding relate to the JavaScript prototype chain?

Class methods are placed on prototypes, and instances access them through the prototype chain. When a subclass defines a method with the same property name, that method exists on the subclass prototype and is found before the parent prototype. Therefore the subclass implementation is selected for ordinary calls on subclass instances. This is a consequence of JavaScript's property lookup model rather than a separate overriding mechanism.

How would you override a method while preserving the parent behavior?

Define the same method in the subclass and call `super` for the parent behavior. The subclass can then modify the returned value, perform additional work, or wrap the parent call with other logic. This keeps the shared behavior centralized in the parent. It also reduces duplication when the parent implementation contains reusable logic.