Interview question
What is method overriding and how does super work with it? Method overriding क्या है और super इसके साथ कैसे काम करता है?
Answer
Method overriding lets a subclass provide its own implementation of a method inherited from a parent class. The super keyword allows calling the parent's version from within the override.
class Employee {
constructor(name, salary) {
this.name = name;
this.salary = salary;
}
getDetails() {
return `${this.name}: $${this.salary}`;
}
calculateBonus() {
return this.salary * 0.1; // default 10% bonus
}
}
class Manager extends Employee {
constructor(name, salary, teamSize) {
super(name, salary);
this.teamSize = teamSize;
}
// OVERRIDING getDetails - completely replaces parent behavior
getDetails() {
return `${super.getDetails()}, manages ${this.teamSize} people`;
// super.getDetails() calls the PARENT's version, then extends it
}
// OVERRIDING calculateBonus with different logic entirely
calculateBonus() {
const baseBonus = super.calculateBonus(); // reuse parent's calculation
const managerBonus = this.teamSize * 100; // add manager-specific bonus
return baseBonus + managerBonus;
}
}
const emp = new Employee('John', 50000);
console.log(emp.getDetails()); // John: $50000
console.log(emp.calculateBonus()); // 5000
const mgr = new Manager('Jane', 80000, 5);
console.log(mgr.getDetails()); // Jane: $80000, manages 5 people
console.log(mgr.calculateBonus()); // 8000 + 500 = 8500
// Constructor chaining - super() MUST be called before using 'this' in a subclass
class Base {
constructor() { this.type = 'base'; }
}
class Derived extends Base {
constructor() {
// this.type = 'derived'; // ReferenceError if called before super()
super(); // must come first
this.type = 'derived'; // now safe to use 'this'
}
}Method overriding subclass को parent class से inherited method का अपना implementation देने देता है। super keyword override के अंदर से parent की version call करने देता है।
class Employee {
constructor(name, salary) {
this.name = name;
this.salary = salary;
}
getDetails() {
return `${this.name}: $${this.salary}`;
}
calculateBonus() {
return this.salary * 0.1;
}
}
class Manager extends Employee {
constructor(name, salary, teamSize) {
super(name, salary);
this.teamSize = teamSize;
}
// getDetails OVERRIDE करना
getDetails() {
return `${super.getDetails()}, manages ${this.teamSize} people`;
}
// calculateBonus OVERRIDE करना
calculateBonus() {
const baseBonus = super.calculateBonus(); // parent का calculation reuse
const managerBonus = this.teamSize * 100;
return baseBonus + managerBonus;
}
}
const emp = new Employee('John', 50000);
console.log(emp.getDetails()); // John: $50000
const mgr = new Manager('Jane', 80000, 5);
console.log(mgr.getDetails()); // Jane: $80000, manages 5 people
console.log(mgr.calculateBonus()); // 8500
// super() 'this' use करने से पहले call होना ज़रूरी है
class Base {
constructor() { this.type = 'base'; }
}
class Derived extends Base {
constructor() {
super(); // पहले आना चाहिए
this.type = 'derived';
}
}Was this answer clear?