Can JavaScript parent and child classes have a method with the same name?

Yes, parent and child classes can have a method with the same name.


class Parent {
   constructor(parentValue) {
      this.parentValue = parentValue;
   //Parent class method name which is same as Child Class method name.
   showValues() {
      console.log("The parent method is called.....");
      console.log("the value is="+this.parentValue);
class Child extends Parent {
      this.childValue = childValue;
   //Child class method name which is same as Parent Class method name.
   showValues() {
      console.log("The child method is called.....");
      console.log("The value is="+`${this.childValue}`);
var parentObject = new Parent(100);
var childObject = new Child(400,500);

To run the above program, you need to use the following command −

node fileName.js.

Here, my file name is demo195.js.


This will produce the following output −

PS C:\Users\Amit\javascript-code> node demo195.js
The parent method is called.....
the value is=100
The child method is called.....
The value is=500