@@ -11,145 +11,138 @@ Polymorphism
11
11
Abstraction
12
12
*/
13
13
14
-
15
14
/*
16
15
✅ 1. Classes & Objects
17
16
Classes
18
17
A blueprint for creating objects (like a template).
19
18
*/
20
19
class Person {
21
- constructor ( name , age ) {
22
- this . name = name ; // Property
23
- this . age = age ;
24
- }
25
-
26
- greet ( ) { // Method
27
- console . log ( `Hello, my name is ${ this . name } and I am ${ this . age } years old.` ) ;
28
- }
20
+ constructor ( name , age ) {
21
+ this . name = name ; // Property
22
+ this . age = age ;
23
+ }
24
+
25
+ greet ( ) {
26
+ // Method
27
+ console . log (
28
+ `Hello, my name is ${ this . name } and I am ${ this . age } years old.` ,
29
+ ) ;
30
+ }
29
31
}
30
32
31
33
// Creating Objects (instances)
32
- const person1 = new Person ( ' Alice' , 30 ) ;
33
- const person2 = new Person ( ' Bob' , 25 ) ;
34
+ const person1 = new Person ( " Alice" , 30 ) ;
35
+ const person2 = new Person ( " Bob" , 25 ) ;
34
36
35
37
person1 . greet ( ) ; // Hello, my name is Alice and I am 30 years old.
36
38
person2 . greet ( ) ; // Hello, my name is Bob and I am 25 years old.
37
39
38
-
39
-
40
40
/*
41
41
✅ 2. Encapsulation
42
42
Hiding the internal state of an object and only exposing necessary parts.
43
43
In JavaScript, we use private fields (#) to implement encapsulation.
44
44
*/
45
45
class Car {
46
- #engine; // Private property
46
+ #engine; // Private property
47
47
48
- constructor ( model , engine ) {
49
- this . model = model ;
50
- this . engine = engine ;
51
- }
48
+ constructor ( model , engine ) {
49
+ this . model = model ;
50
+ this . engine = engine ;
51
+ }
52
52
53
- startEngine ( ) {
54
- console . log ( `Starting the ${ this . #engine} engine of ${ this . model } .` ) ;
55
- }
53
+ startEngine ( ) {
54
+ console . log ( `Starting the ${ this . #engine} engine of ${ this . model } .` ) ;
55
+ }
56
56
}
57
57
58
- const myCar = new Car ( ' Tesla' , ' Electric' ) ;
58
+ const myCar = new Car ( " Tesla" , " Electric" ) ;
59
59
myCar . startEngine ( ) ; // Startng the Electric engine of Tesla.
60
60
console . log ( myCar . #engine) ; // Error private field
61
61
62
-
63
-
64
62
/*
65
63
✅ 3. Inheritance
66
64
Creating new classes from existing ones (Parent-Child relationship).
67
65
Super keyword is used to call the constructor of the parent class.
68
66
*/
69
67
class Animal {
70
- constructor ( name ) {
71
- this . name = name ;
72
- }
68
+ constructor ( name ) {
69
+ this . name = name ;
70
+ }
73
71
74
- makeSound ( ) {
75
- console . log ( `${ this . name } makes a sound.` ) ;
76
- }
72
+ makeSound ( ) {
73
+ console . log ( `${ this . name } makes a sound.` ) ;
74
+ }
77
75
}
78
76
79
77
class Dog extends Animal {
80
- constructor ( name , breed ) {
81
- super ( name ) ; // Call the parent contructor
82
- this . breed = breed ;
83
- }
84
-
85
- makeSound ( ) {
86
- console . log ( `${ this . name } barks.` ) ;
87
- }
78
+ constructor ( name , breed ) {
79
+ super ( name ) ; // Call the parent contructor
80
+ this . breed = breed ;
81
+ }
82
+
83
+ makeSound ( ) {
84
+ console . log ( `${ this . name } barks.` ) ;
85
+ }
88
86
}
89
87
90
- const dog1 = new Dog ( ' Buddy' , ' Golden Retriever' ) ;
88
+ const dog1 = new Dog ( " Buddy" , " Golden Retriever" ) ;
91
89
dog1 . makeSound ( ) ; // Buddy barks.
92
90
93
-
94
-
95
91
/*
96
92
✅ 4. Polymorphism
97
93
The ability of objects to take on many forms.
98
94
A child class can have a method with the same name as the parent class (Method Overriding).
99
95
*/
100
96
class Shape {
101
- draw ( ) {
102
- console . log ( ' Drawing a shape.' ) ;
103
- }
97
+ draw ( ) {
98
+ console . log ( " Drawing a shape." ) ;
99
+ }
104
100
}
105
101
106
102
class Circle extends Shape {
107
- draw ( ) {
108
- console . log ( ' Drawing a circle.' ) ;
109
- }
103
+ draw ( ) {
104
+ console . log ( " Drawing a circle." ) ;
105
+ }
110
106
}
111
107
112
108
class Square extends Shape {
113
- draw ( ) {
114
- console . log ( ' Drawing a square.' ) ;
115
- }
109
+ draw ( ) {
110
+ console . log ( " Drawing a square." ) ;
111
+ }
116
112
}
117
113
118
114
const shapes = [ new Circle ( ) , new Square ( ) , new Shape ( ) ] ;
119
115
120
- shapes . forEach ( shape => shape . draw ( ) ) ;
116
+ shapes . forEach ( ( shape ) => shape . draw ( ) ) ;
121
117
// Output:
122
118
// Drawing a circle.
123
119
// Drawing a square.
124
120
// Drawing a shape.
125
121
126
-
127
-
128
-
129
122
/*
130
123
✅ 5. Abstraction
131
124
Hiding complexity by exposing only necessary details.
132
125
JavaScript doesn't have true abstraction but we can achieve it using classes, interfaces (via TypeScript), and encapsulation.
133
126
*/
134
127
class User {
135
- constructor ( username , password ) {
136
- this . username = username ;
137
- this . #password = password ; // Private field
138
- }
139
-
140
- #encryptPassword( ) { // Private method
141
- return this . #password. split ( '' ) . reverse ( ) . join ( '' ) ;
142
- }
143
-
144
- getEncryptedPassword ( ) {
145
- return this . #encryptPassword( ) ;
146
- }
128
+ constructor ( username , password ) {
129
+ this . username = username ;
130
+ this . #password = password ; // Private field
131
+ }
132
+
133
+ #encryptPassword( ) {
134
+ // Private method
135
+ return this . #password. split ( "" ) . reverse ( ) . join ( "" ) ;
136
+ }
137
+
138
+ getEncryptedPassword ( ) {
139
+ return this . #encryptPassword( ) ;
140
+ }
147
141
}
148
142
149
- const user1 = new User ( ' Alice' , ' mypassword' ) ;
143
+ const user1 = new User ( " Alice" , " mypassword" ) ;
150
144
console . log ( user1 . getEncryptedPassword ( ) ) ; // drowssapym
151
145
152
-
153
146
/*
154
147
🔥 Summary of Important Things to Remember:
155
148
Classes are templates for creating objects.
@@ -160,18 +153,17 @@ Abstraction simplifies usage by hiding complex details.
160
153
Getters & Setters provide controlled access to properties.
161
154
*/
162
155
163
-
164
156
// INFO: First Quiz
165
157
class Car {
166
- constructor ( brand , model ) {
167
- this . brand = brand ;
168
- this . model = model ;
169
- } ;
170
-
171
- displayInfo ( ) {
172
- console . log ( `Brand: [${ this . brand } ], Model: [${ this . model } ]` )
173
- } ;
158
+ constructor ( brand , model ) {
159
+ this . brand = brand ;
160
+ this . model = model ;
161
+ }
162
+
163
+ displayInfo ( ) {
164
+ console . log ( `Brand: [${ this . brand } ], Model: [${ this . model } ]` ) ;
165
+ }
174
166
}
175
167
176
- const car = new Car ( ' Toyota' , ' Coralla' ) ;
168
+ const car = new Car ( " Toyota" , " Coralla" ) ;
177
169
car . displayInfo ( ) ; // Brand: [Toyota], Model: [Coralla]
0 commit comments