-
Notifications
You must be signed in to change notification settings - Fork 66
/
02_inheritance.js
93 lines (72 loc) · 2.58 KB
/
02_inheritance.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Base / Parent / Super class
class Shape {
// constructor with color property
constructor(color) {
this.color = color;
}
// Method to paint shape with color
paint() {
console.log(`Painting with color ${this.color}`);
}
// Method to calculate area of shape
area() {
throw new Error("The area method must be implemented in the subclass");
}
// Method to get description of shape with color property
getDescription() {
return `A shape with color ${this.color}`;
}
}
// Derived / Child / Sub class
class Rectangle extends Shape {
// constructor with width, height and color properties
constructor(width, height, color) {
super(color); // Call the parent class constructor to set the color
this.width = width;
this.height = height;
}
// Method to calculate area of rectangle
area() {
return this.width * this.height;
}
// Method to get description of rectangle with width, height and color properties
getDescription() {
return `A rectangle with width ${this.width}, height ${this.height}, and color ${this.color}`;
}
}
// Derived / Child / Sub class
class Circle extends Shape {
// constructor with radius and color properties
constructor(radius, color) {
super(color); // Call the parent class constructor to set the color
this.radius = radius;
}
// Method to calculate area of circle
area() {
return Math.PI * this.radius * this.radius;
}
// Method to get description of circle with radius and color properties
getDescription() {
return `A circle with radius ${this.radius} and color ${this.color}`;
}
}
// Create a instance/object of Rectangle class
const rect1 = new Rectangle(2, 4, "red");
// Call area method using the instance and store the result in a variable
const area1 = rect1.area();
// Log the area of the rectangle
console.log(area1); // Output: 8
// Call paint method using the instance
rect1.paint(); // Output: Painting with color red
// Log the description of the rectangle
console.log(rect1.getDescription()); // Output: A rectangle with width 2, height 4, and color red
// Create a instance/object of Circle class
const circle1 = new Circle(5, "blue");
// Call area method using the instance and store the result in a variable
const area2 = circle1.area();
// Log the area of the circle
console.log(area2); // Output: 78.53981633974483
// Call paint method using the instance
circle1.paint(); // Output: Painting with color blue
// Log the description of the circle
console.log(circle1.getDescription()); // Output: A circle with radius 5 and color blue