You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
console.log(`${this.name} is a manager in the ${this.department} department.`);
85
+
public getManagerDetails(): void {
86
+
console.log(`${this.name} is a manager in the ${this.department} department.`);
87
+
}
87
88
}
88
-
}
89
89
90
-
let manager = new Manager("Bob", 80000, "HR");
91
-
manager.getDetails(); // Output: Bob works in the HR department.
92
-
// manager.getSalary(); // Error: Property 'getSalary' is private and only accessible within class 'Employee'.
93
-
manager.getManagerDetails(); // Output: Bob is a manager in the HR department.
94
-
```
90
+
let manager = new Manager("Bob", 80000, "HR");
91
+
manager.getDetails(); // Output: Bob works in the HR department.
92
+
// manager.getSalary(); // Error: Property 'getSalary' is private and only accessible within class 'Employee'.
93
+
manager.getManagerDetails(); // Output: Bob is a manager in the HR department.
94
+
```
95
95
96
96
## 3. The readonly Modifier
97
97
98
98
Use the readonly modifier to make class properties immutable. Once assigned, the value of readonly properties cannot be changed, ensuring that certain critical properties remain constant.
car.displayDetails(); // Output: 2020 Toyota Corolla
121
-
car.year = 2021;
122
-
car.displayDetails(); // Output: 2021 Toyota Corolla
123
-
// car.make = "Honda"; // Error: Cannot assign to 'make' because it is a read-only property.
124
-
```
119
+
let car = new Car("Toyota", "Corolla", 2020);
120
+
car.displayDetails(); // Output: 2020 Toyota Corolla
121
+
car.year = 2021;
122
+
car.displayDetails(); // Output: 2021 Toyota Corolla
123
+
// car.make = "Honda"; // Error: Cannot assign to 'make' because it is a read-only property.
124
+
```
125
125
126
126
## 4. Getters & Setters
127
127
128
128
Getters and Setters allow you to control the access of the class properties. They enable the encapsulation of data, providing controlled access and modification of properties.
// circle.radius = -5; // Error: Radius must be positive.
161
+
```
162
162
163
163
## 5. Inheritance
164
164
165
165
Learn how to use inheritance to reuse the functionality of another class. Inheritance allows you to create a new class based on an existing class, extending its properties and methods.
166
166
167
167
Example:
168
168
169
-
```<js title='typescript'>
170
-
class Animal {
171
-
name: string;
169
+
```tsx title='typescript'
170
+
class Animal {
171
+
name: string;
172
172
173
-
constructor(name: string) {
174
-
this.name = name;
175
-
}
173
+
constructor(name: string) {
174
+
this.name = name;
175
+
}
176
176
177
-
move(distance: number = 0): void {
178
-
console.log(`${this.name} moved ${distance} meters.`);
177
+
move(distance: number = 0): void {
178
+
console.log(`${this.name} moved ${distance} meters.`);
179
+
}
179
180
}
180
-
}
181
181
182
-
class Dog extends Animal {
183
-
bark(): void {
184
-
console.log("Woof! Woof!");
182
+
class Dog extends Animal {
183
+
bark(): void {
184
+
console.log("Woof! Woof!");
185
+
}
185
186
}
186
-
}
187
187
188
-
let dog = new Dog("Rex");
189
-
dog.bark(); // Output: Woof! Woof!
190
-
dog.move(10); // Output: Rex moved 10 meters.
191
-
```
188
+
let dog = new Dog("Rex");
189
+
dog.bark(); // Output: Woof! Woof!
190
+
dog.move(10); // Output: Rex moved 10 meters.
191
+
```
192
192
193
193
## 6. Static Methods & Properties
194
194
195
195
Define static methods and properties shared by all instances of a class. Static members belong to the class itself rather than to any specific instance, making them useful for utility functions or shared data.
196
196
197
197
Example:
198
198
199
-
```<js title='typescript'>
200
-
class MathUtil {
201
-
static PI: number = 3.14;
199
+
```tsx title='typescript'
200
+
class MathUtil {
201
+
static PI: number = 3.14;
202
202
203
-
static calculateCircumference(radius: number): number {
204
-
return 2 * MathUtil.PI * radius;
203
+
static calculateCircumference(radius: number): number {
Explain the abstract classes that define some common behaviors. Abstract classes cannot be instantiated directly and are meant to be extended by other classes, providing a base structure and forcing implementation of specific methods.
215
215
216
-
Example:
216
+
Example -
217
217
218
-
```<js title='typescript'>
219
-
abstract class Shape {
220
-
abstract getArea(): number;
218
+
```tsx title='typescript'
219
+
abstract class Shape {
220
+
abstract getArea(): number;
221
221
222
-
display(): void {
223
-
console.log(`The area is ${this.getArea()}`);
222
+
display(): void {
223
+
console.log(`The area is ${this.getArea()}`);
224
+
}
224
225
}
225
-
}
226
226
227
-
class Rectangle extends Shape {
228
-
width: number;
229
-
height: number;
227
+
class Rectangle extends Shape {
228
+
width: number;
229
+
height: number;
230
230
231
-
constructor(width: number, height: number) {
232
-
super();
233
-
this.width = width;
234
-
this.height = height;
235
-
}
231
+
constructor(width: number, height: number) {
232
+
super();
233
+
this.width = width;
234
+
this.height = height;
235
+
}
236
236
237
-
getArea(): number {
238
-
return this.width * this.height;
237
+
getArea(): number {
238
+
return this.width * this.height;
239
+
}
239
240
}
240
-
}
241
241
242
-
let rectangle = new Rectangle(5, 10);
243
-
rectangle.display(); // Output: The area is 50
244
-
```
242
+
let rectangle = new Rectangle(5, 10);
243
+
rectangle.display(); // Output: The area is 50
244
+
```
245
245
246
246
These concepts and examples illustrate how to effectively use classes and their advanced features in TypeScript, enhancing your ability to write robust and maintainable object-oriented code.
0 commit comments