forked from Swap76/Learn-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinheritance.js
More file actions
35 lines (26 loc) · 810 Bytes
/
inheritance.js
File metadata and controls
35 lines (26 loc) · 810 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// common functions are combined in a class to have less repetition in code
class Animals { // Animal class is created where the legs are defined 4
constructor () {
this.legs = 4;
}
getLegs () { // Function for getting legs
return (this.legs);
}
}
class Dog extends Animals {
constructor (age) {
super(); // As We have to also call the constructor of Animals if constructor is empty this automatically calls the super
this.age = age;
}
// we have inherited the animals so getLegs method is also there in Dog class
getSound () {
return ("Bhow");
}
getAge () {
return (this.age);
}
}
const tommy = new Dog(10); // Created Object of Dog class
console.log(tommy.getLegs()); // Output 4
console.log(tommy.getSound()); // Bhow
console.log(tommy.getAge()); // 10