Method Overriding in JavaScript Functions

When working with object-oriented programming in JavaScript, it’s common to come across a scenario where you need to override a method inherited from a parent class in a child class. This is known as method overriding.

What is Method Overriding?

Method overriding is a concept in object-oriented programming where a subclass provides its own implementation of a method that is already defined in its parent class. This allows the subclass to modify the behavior of the inherited method without changing its signature.

Implementing Method Overriding in JavaScript

In JavaScript, you can achieve method overriding through prototype-based inheritance. The prototype object is a shared object among all instances created using a constructor function. By modifying the prototype object of a child class, you can override a method inherited from the parent class.

Here is an example demonstrating method overriding in JavaScript:

// Parent class
function Animal(name) {
  this.name = name;
}

// Method in parent class
Animal.prototype.makeSound = function() {
  console.log(this.name + ' makes a sound');
};

// Child class
function Dog(name) {
  Animal.call(this, name);
}

// Inherit prototype from parent class
Dog.prototype = Object.create(Animal.prototype);
Dog.prototype.constructor = Dog;

// Overridden method in child class
Dog.prototype.makeSound = function() {
  console.log(this.name + ' barks');
};

// Create instances
const animal = new Animal('Generic Animal');
const dog = new Dog('Fido');

// Call methods
animal.makeSound();  // Output: Generic Animal makes a sound
dog.makeSound();     // Output: Fido barks

In the example above, we have a parent class Animal with a method makeSound. The child class Dog inherits from the Animal class and overrides the makeSound method to make the dog “bark” instead of a generic sound.

Conclusion

Method overriding in JavaScript allows you to modify the behavior of an inherited method in a child class. By using prototype-based inheritance, you can override methods and customize functionality according to your needs. Understanding and implementing method overriding is crucial when working with complex object-oriented programming in JavaScript.

#JavaScript #MethodOverriding