-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11_Inheritance.ts
More file actions
81 lines (63 loc) · 2.65 KB
/
Copy path11_Inheritance.ts
File metadata and controls
81 lines (63 loc) · 2.65 KB
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
/*
=> Inheritance :
Inheritance is the process of defining a new object with the help of an existing object
Accessing existing objects functionality.
Updating existing objects functionality.
Re-Usability & IS-A relation
IS-A is a term to refer to inheritance.
*/
// => Extending a class :
// Parent class ->
class BMW {
make : string;
model : string;
manufacturing_year : number;
constructor(make : string, model : string, manufacturing_year : number){
this.make = make;
this.model = model;
this.manufacturing_year = manufacturing_year;
}
commonEnginefunctionality(){
console.log("Common Functionality");
}
start(){
console.log("Start");
}
stop(){
console.log("Stop");
}
}
// Child class 1 ->
class ThreeSeries extends BMW {
cruiseControlEnabled : boolean;
// Initializing the properties using "constructor" -
constructor(make : string, model : string, manufacturing_year : number, cruiseControlEnabled : boolean){
// Constructors for derved classes must contain a "super" call -
super(make, model, manufacturing_year) // ***** invoke all the parent class properties.
// 'super' automatically calls the parent class constructor and all the parent class properties.
this.cruiseControlEnabled = cruiseControlEnabled;
}
}
// Child class 2 ->
class FiveSeries extends BMW {
ParkingAssistEnabled : boolean;
constructor(make : string, model : string, manufacturing_year : number, ParkingAssistEnabled: boolean){
// Constructors for derved classes must contain a "super" call -
super(make, model, manufacturing_year) // ***** invoke all the parent class properties.
// 'super' automatically calls the parent class constructor and all the parent class properties.
this.ParkingAssistEnabled = ParkingAssistEnabled;
}
}
// Creating objects of these classes :
// Object 1 ->
var Object_Model_ThreeSeries = new ThreeSeries('BMW', '328i', 2012, false) // the object is created.
console.log(Object_Model_ThreeSeries.make);
console.log(Object_Model_ThreeSeries.model);
console.log(Object_Model_ThreeSeries.manufacturing_year);
console.log(Object_Model_ThreeSeries.cruiseControlEnabled);
// Object 2 ->
var Object_Model_FiveSeries = new FiveSeries('BMW', '535i', 2012, false) // the object is created.
console.log(Object_Model_FiveSeries.make);
console.log(Object_Model_FiveSeries.model);
console.log(Object_Model_FiveSeries.manufacturing_year);
console.log(Object_Model_FiveSeries.ParkingAssistEnabled);