Sobes.tech
Junior — Middle

Explain what overriding functions means in object-oriented programming.

sobes.tech AI

Answer from AI

Method overriding in object-oriented programming is a mechanism where a subclass provides its own implementation of a method that is already defined in its superclass.

This allows changing or extending the behavior of inherited methods for specific subclasses.

For example, in JavaScript (Node.js), it looks like this:

class Animal {
  speak() {
    console.log('Animal makes a sound');
  }
}

class Dog extends Animal {
  speak() {
    console.log('Dog barks');
  }
}

const dog = new Dog();
dog.speak(); // Outputs: Dog barks

Here, the speak() method is overridden in the Dog class to change the behavior inherited from Animal.