-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPolymorphism.js
44 lines (36 loc) · 1.11 KB
/
Polymorphism.js
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
36
37
38
39
40
41
42
43
44
//Polymorphism
//Polymorphism is a concept that utilizes inheritance for reusing methods multiple times with a different behaviour depending on class types. To understand this lets look at our example -in the dog class we will remove the bark method and in the animal class we'lladd a make Sound method which will be overridden by cat and dog classes.
class Animal {
constructor(name, age) {
this.name = name;
this.age = age;
}
makeSound() {
return (
`Some nice sound made`
)
}
}
class Dog extends Animal {
constructor(name, age, breed) {
super(name, age);
this.breed = breed;
}
makeSound() {
return 'Bark';
}
}
//------------------Polymorphism------------------
class Cat extends Animal {
constructor(name, age, weight) {
super(name, age);
this.weight = weight;
}
makeSound() {
return 'Meow';
}
}
const myDog = new Dog('Rex', 2, 'Sh ');
const myCog = new Dog('whiskers', 1, '5kg');
console.log(myDog.makeSound());
console.log(myCog.makeSound());