-
Notifications
You must be signed in to change notification settings - Fork 0
/
034-Class-Inheritance.js
48 lines (39 loc) · 1.25 KB
/
034-Class-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
/*
* Author : Jaydatt Patel
Inheritance :
- You can derive new class from base or super class using 'extends' keyword.
- You can inhetits all Properties of super class but private properties can not be inherit.
- You can access method or variable of super class in derived class using 'super' keyword.
- You can also add prototype for class same as constructor function.
syntax:
class derived_class extends base_class{
....
}
*/
class Person {
#type = "person";
constructor(name, age, gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
show() {
return `${this.name}, ${this.age}, ${this.gender}`;
}
}
class Student extends Person {
constructor(roll, course, name, age, gender) {
super(name, age, gender); //calling base class constructor
this.roll = roll;
this.course = course;
// this.#type = 'student'; // Error: Private variable of base class
}
show() {
// calling base class method which is overridden
return super.show() + `, ${this.roll}, ${this.course}`;
}
}
console.log("-------------------------------");
var st1 = new Student(101, "Computer", "Amit", 22, "Male");
console.log(st1.show());
console.log(st1);