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*/
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.
1public static void printListObject(List<Object> list) {
2 for (Object element : list) {
3 System.out.print(element + " ");
4 }
5}
6
7public static void printListWildCard(List<?> list) {
8 for (Object element: list) {
9 System.out.print(element + " ");
10 }
11}
12