extends in javascript

Solutions on MaxInterview for extends in javascript by the best coders in the world

showing results for - "extends in javascript"
Agustina
23 Nov 2019
1The extends keyword is used to create a child class of another class (parent). 
2The child class inherits all the methods from another class. Inheritance is 
3useful for code reusability: reuse properties and methods of an existing class 
4when you create a new class.
Cecily
24 Feb 2019
1// parent class animal
2class Animal {
3    constructor(name, weight) {
4        this.name = name;
5        this.weight = weight;
6    }
7
8    eat() {
9        return `${this.name} is eating!`;
10    }
11
12    sleep() {
13        return `${this.name} is going to sleep!`;
14    }
15
16    wakeUp() {
17        return `${this.name} is waking up!`;
18    }
19
20}
21//sub class gorilla
22
23class Gorilla extends Animal {
24    constructor(name, weight) {
25        super(name, weight);
26    }
27
28    climbTrees() {
29        return `${this.name} is climbing trees!`;
30    }
31
32    poundChest() {
33        return `${this.name} is pounding its chest!`;
34    }
35
36    showVigour() {
37        return `${super.eat()} ${this.poundChest()}`;
38    }
39
40    dailyRoutine() {
41        return `${super.wakeUp()} ${this.poundChest()} ${super.eat()} ${super.sleep()}`;
42    }
43
44}
45
46function display(content) {
47    console.log(content);
48}
49
50const gorilla = new Gorilla('George', '160Kg');
51display(gorilla.poundChest());
52display(gorilla.sleep());
53display(gorilla.showVigour());
54display(gorilla.dailyRoutine());
55
56// OUTPUT:
57// George is pounding its chest!
58// George is going to sleep!
59// George is eating! George is pounding its chest!
60// George is waking up! George is pounding its chest! George is eating! G
61/*
62The above code has 2 JavaScript classes namely Animal and Gorilla.
63The Gorilla class is a subclass or child class of Animal and it uses the extends keyword to set itself as a subclass.
64However, the super keyword has been used in two different ways.
65Did you guys notice the same? In Gorilla’s constructor (line 23 of code)
66super is used as a “function”. Whereas, Gorilla’s showVigour() (line 35) and dailyRoutine()(line 39) methods have used super as an “object”.
67The super keyword is used in two ways because of the following reasons:
68In line 24, the super keyword is used as a “function” which calls the
69parent class Animal with the parameters passed to Gorilla. 
70This is a key step to be carried out in order to make sure that
71Gorilla is an instance of Animal.
72In line 36 and 40 super is used as an “object” which
73refers to an Animal instance (parent class). The super keyword 
74here is used to call the methods of the parent class Animal explicitly.
75People familiar with languages like C#, Java, Python can pretty
76much relate to how all this works. However, JavaScript was not so simple before ES6 came in, especially for classes. So how did people code without using class syntax, super and extends keywords? Or they never used such concepts before and suddenly decided to add them? Let’s find out!
77*/
Giacomo
03 Nov 2017
1The extends keyword is used to create a child class of another class (parent). The child class inherits all the methods from another class. Inheritance is useful for code reusability: reuse properties and methods of an existing class when you create a new class.
queries leading to this page
object inheritance in javascriptexpress extend classextend class javascript es6extedn class syntax jsclass extending jsjs extends functionjavascript inheritance examplewhen to use extends javascriptjavascript extend classhow does extends work in jsjavascript extends objectinheritance in javascript with exampleinheritance example javascriptextends keyword in javascriptextends keyword in jsjs child classexample extends jsjavascript extend new classjava script extendsextended class use method from extender javascriptextending class jsinheritance using constructor in javascriptjavascript inheritance w3schoolsextends in es6how to make inheritance in javascriptclass extends js getjavascript class extends classclass extends java scripthow inheritance works in javascriptjavascript class extensionsextends jsjavascript subclass vs extensionhow to extend class in nodejsextend javascript classextends in new javascriptjavascript extends with classusing extends jsextendsjavascript extend class has to have methodhow to extend a class in javascriptjavascript extend from classextended class use something from extender javascripttypes of inheritance in javascriptjavascript class extensionwhat is extends in javascriptextended class constructor jsdoes javascript use extends keywordhow inheritance works in javascript using functiones6 extendsextends in class javascriptjavascript class extends not workinginheritance in prototype javascriptdoes javascript support inheritanceextends keyword javascriptexplain extends keyword in javascripthow to use extends in jssubclass javascriptextends keyword in reactjavascript class and extends 24 extend in javascriptextend vs inherit javascriptextending classes javascriptextend class constructor javascriptextends function in javascriptwhat happens when extends in javascripthow to extend a javascript classjs function extendsfunction extends javascriptjavascript es6 extending a classcreate subclass jsjs extendsinheritance javascriptextend class in javascriptto extend the class methods to new class in javascritpinheritance from one clas to another class in javascriptinheritance in object in jshow use class extends jsmust extend a class in jsjavascript extends functionjavascript extend a class 7e javascript class extend exampleextent calsss javascriptcreate subclass jssjavascript class extends examplejs extend classwhat is extend in es6inheritance by call 28 29 javascriptsubclass in jsunextend class jswe use the extends keyword to create a subclass javascriptuses of extends in jsjs extend class need constructorjavascript class extendsextends keyword jsclass extends jsjs extend function classthe extends keyword in javascript explainedextend a class in jshow to extend in jsextend classes javascriptextend javascriptjs extemdswhat does extends in javascript meanextends definition javascriptextends javascriptinheritance in javascript w3schoolsextend in javascripthow to use extends in javascriptextend js classextendable classes javascript extends jsjavascript inheritance isjavascript extend class thisclass extends javascriptconstructor call extend jsclass extension jsclass extends in jshow to extend classes in javascriptclass extends another class in javascripthow to use a class extending another class javascriptjavascript prototype inheritancefunctional inheritance in javascriptextends in javascriptinheritance extends and superjavascript class apply extendjavascript extendsjavascript inheritancejavascript nodejs extendsinheritance in javascriptinheritance in javascript example w3schoolsglobal class extends in jsjavascript subclassextends method javascriptextend class jsjavascript extending classesjavascript extends explainedwhich of the following keywords is used to indicate inheritance in javascriptjavascript extendsionwhat does extends do in jsjavascript extends classnodejs class extendshow to set a class must extends another class in jsextend keyword javascriptdose javscript have class extendswhat is extends javascriptextends property in javascriptjs subclasseshow to extend class jsinline extend parent class javascriptfunction extends jsjavascript 22extends 22 methodextends in javascriotjs extending classmake subclass jssubclass in javascriptextends jasjs class extendsjs extend constructorwhy class extends objectclass jvascriptinheritance in function javascripthow exactly extend keyword works in javascriptinheritance jsextends in class js how does extends work in javascriptclass extend javascriptdo we need initialze if extens in java javascriptextends function javascriptextends vs inheritance javascriptextends keyword meaning in node jsextend a class in javascriptextends meaning javascriptwhat is inheritance in javascriptjavascript extendsjs inheritanceextends es6 javascriptjs extends option valuesjs es6 extendsextends javascritpclass extends in javascriptwhat does extends keyword in javascriptclass extend class javasciriptclass extends package javascriptclass extends function javascriptjs class properties access extend function extends class in javascriptnode js how to use function of extended classjavascript extands classjs get extend name classjavascript extends class methodclass extendsinheritance in javascript es6what does extends do in javascript 22extends 22 in javascripthow to extned to instance of class jsjavascript extends methodclass javascript extendsjavascript extends functionextends class javascriptsubclass jssubclasses in javascriptwhat does extends mean in javascriptextends function jsinheritance in jsclass extend java scriptextend class javascriptjavascript class extendextends in jsextends java scriptclass inheritance in jshow to extend class in jsfunction extends class javascriptextend a calss jsextends in javascript