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
33 lines (26 loc) · 798 Bytes
/
Copy pathinheritance.js
File metadata and controls
33 lines (26 loc) · 798 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
// 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);
}
}
let 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