- Home
- /
- Tutorials
- /
- JavaScript Tutorial
- /
- JavaScript Method Overriding
JavaScript Classes
JavaScript Method Overriding
A child class can replace a method it inherited with its own version.
Define a method with the same name and the child's version wins.
This is what lets different classes respond to the same call in their own way.
Example
Example
javascript
class Animal {
speak() {
return "some sound";
}
}
class Dog extends Animal {
speak() {
return "woof";
}
}
console.log(new Dog().speak());