Skip to content

Method Overriding

Method overriding is a feature where a child class provides its own implementation of a method that is already defined in its parent class. It’s a form of run-time polymorphism (dynamic dispatch).

  • To allow child classes to customize inherited behavior
  • To implement polymorphism — same method name, different behavior
  • To follow the Open/Closed Principle — extend behavior without modifying parent

Real-World Analogy: Generic vs Specific Sounds

Section titled “Real-World Analogy: Generic vs Specific Sounds”

A generic “Animal” makes a generic sound. But specific animals override that:

  • Animal.speak() → “Some sound”
  • Dog.speak() → “Woof!” (overrides)
  • Cat.speak() → “Meow!” (overrides)

Same method name (speak), different implementations.


Method overriding is the process of redefining a method in a child class that already exists in the parent class with the same signature (name and parameters).


sequenceDiagram
participant Client
participant Dog as Dog Instance
participant Parent as Animal (Parent)
Client->>Dog: dog.speak()
Dog->>Dog: Check if Dog has speak()
Dog->>Dog: ✅ Found in Dog
Dog-->>Client: "Woof!"
Client->>Cat: cat.speak()
Cat->>Cat: Check if Cat has speak()
Cat->>Cat: ✅ Found in Cat
Cat-->>Client: "Meow!"
Note over Dog,Parent: If Dog didn't override,<br/>would use Animal's speak()

class Employee {
constructor(name, salary) {
this.name = name;
this.salary = salary;
}
work() {
return `${this.name} is working`;
}
getRole() {
return "Employee";
}
}
class Manager extends Employee {
constructor(name, salary, department) {
super(name, salary);
this.department = department;
}
// Override work()
work() {
return `${this.name} is managing the ${this.department} department`;
}
// Override getRole()
getRole() {
return "Manager";
}
// Extend — call parent method then add more
getCompensation() {
const base = super.getCompensation(); // Call parent
const bonus = this.salary * 0.2;
return `${base} + $${bonus} bonus`;
}
}
// Polymorphism in action
const employees = [
new Employee("Generic", 50000),
new Manager("Alice", 80000, "Engineering")
];
employees.forEach(e => {
console.log(`${e.getRole()}: ${e.work()}`);
// "Employee: Generic is working"
// "Manager: Alice is managing the Engineering department"
});

AspectOverridingOverloading
RelationshipParent → ChildSame class
Method signatureSameDifferent
Polymorphism typeRun-timeCompile-time
PurposeSpecialize behaviorConvenience API

RuleDescription
Same signatureMethod name and parameters must match
InheritanceMust be a parent-child relationship
Access levelCannot be more restrictive
Final methodsCannot be overridden if marked final
Static methodsCannot be overridden (can be hidden)
ConstructorsCannot be overridden

  • Use @Override annotation (Java) or override keyword for clarity
  • Call super.methodName() when extending, not replacing
  • Keep overridden methods consistent with the parent’s contract (LSP)
  • Don’t override to add completely unrelated behavior

ConceptKey Point
OverridingChild redefines parent method
PolymorphismRun-time (dynamic dispatch)
Same signatureName + parameters must match
superCall parent’s version
RuleDon’t violate LSP when overriding

Previous Topic: Super Keyword → Next Topic: Polymorphism →