- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
Example
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 { constructor(parentValue,childValue){ super(parentValue); 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); parentObject.showValues(); var childObject = new Child(400,500); childObject.showValues();
To run the above program, you need to use the following command −
node fileName.js.
Here, my file name is demo195.js.
Output
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
- Related Articles
- How can we invoke the parent's method, when a child has a method with the same name in JavaScript?
- Parent and Child classes having same data member in Java
- What are Java parent and child classes in Java?
- Can a method have the same name as the class?
- How many public classes of the same name it can have in Java?
- How to get the child element of a parent using JavaScript?
- Apply style to the parent if it has a child with CSS and HTML
- jQuery parent > child Selector
- Parent-Child Interaction Therapy: Meaning And Application
- Can we define a method name same as class name in Java?
- Style every element that is the child of its parent, counting from the last child with CSS
- How to generate child keys by parent keys in array JavaScript?
- Is it possible to have View and table with the same name in MySQL?
- How to return true if the parent element contains the child element in JavaScript?
- How can create a table having the name like a^b along with same column name? name?

Advertisements