- 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
How can we invoke the parent's method, when a child has a method with the same name in JavaScript?
In order to call the parent method when both parent and child have the same method name and signature.
You can use the below syntax −
console.log(yourParentClassName.prototype.yourMethodName.call(yourChildObjectName));
Example
class Super { constructor(value) { this.value = value; } display() { return `The Parent class value is= ${this.value}`; } } class Child extends Super { constructor(value1, value2) { super(value1); this.value2 = value2; } display() { return `${super.display()}, The Child Class value2 is=${this.value2}`; } } var childObject = new Child(10, 20); console.log("Calling the parent method display()=") console.log(Super.prototype.display.call(childObject)); console.log("Calling the child method display()="); console.log(childObject.display());
To run the above program, you need to use the following command −
node fileName.js.
Here, my file name is demo192.js.
Output
This will produce the following output −
PS C:\Users\Amit\javascript-code> node demo192.js Calling the parent method display()= The Parent class value is= 10 Calling the child method display()= The Parent class value is= 10, The Child Class value2 is=20
- Related Articles
- How can we invoke the parent's method, when a child has a method with the same name in JavaScript?
- Can JavaScript parent and child classes have a method with the same name?
- How do I invoke a Java method when given the method name as a string?
- Can we define a method name same as class name in Java?
- How to invoke a JavaScript Function as a method?
- Can a method have the same name as the class?
- When can we use the pack() method in Java?
- When can we use the getClass() method in Java?
- Can we define multiple methods in a class with the same name in Java?
- How to invoke a function as a function and method?
- Can we share a method between JavaScript objects in an array?
- When can we use StackWalker.getCallerClass() method in Java 9?
- How to name a data frame column with a vector value that has the same name in R?
- How does TestNG invoke a test method using multiple threads?
- How can we implement the paintComponent() method of a JPanel in Java?\n
- How can we invoke MySQL stored procedure?

Advertisements