TypeScript Object-Oriented Programming (OOP)
Learn how to use classes, objects, constructors, inheritance, encapsulation, access modifiers, abstract classes and static members in TypeScript.
1. What is OOP in TypeScript?
OOP stands for Object-Oriented Programming. In TypeScript, we use classes to define properties and methods, and objects to work with those properties and methods.
TypeScript supports important OOP concepts such as classes, inheritance, encapsulation, access modifiers and abstract classes.
Example: Student Management
Imagine a student management system. Every student has details such as name and age, and can perform actions such as displaying student information.
class Student {
name: string;
age: number;
constructor(name: string, age: number) {
this.name = name;
this.age = age;
}
displayInfo(): void {
console.log(this.name, this.age);
}
}
const student1 = new Student("Rahul", 22);
student1.displayInfo();
Expected Output
Rahul 22
2. OOP Workflow Diagram
Understand how a class is used to create an object and how that object accesses its properties and methods.
Class
Defines the structure, properties and methods.
Object
Created from the class using its constructor.
Properties
Store information such as name and age.
Methods
Define actions performed by the object.
3. Real-Life Example of Class and Object
Student Class
Common blueprint
Properties
Name
Age
Method: displayInfo()
Object 1
student1
Name: Rahul
Age: 22
Can call displayInfo()
Object 2
student2
Name: Priya
Age: 21
Can call displayInfo()
4. Topics Covered in TypeScript OOP
Learn each concept step by step using explanations, TypeScript code examples and visual diagrams.
1. Classes and Objects
Learn how to define classes, create objects and access members.
2. Constructors
Understand how constructors initialize an object's properties.
3. Inheritance
Reuse class functionality by extending an existing class.
4. Encapsulation
Control access to data and protect an object's internal state.
5. Access Modifiers
Use public, private and protected to control member accessibility.
6. Abstract Classes
Define a common base class that other classes can extend.
7. Static Members
Understand static properties and methods accessed through the class itself.
5. Advantages of OOP
Code Reusability
Reuse existing functionality through classes and inheritance.
Data Protection
Use encapsulation and access modifiers to control data access.
Code Organization
Organize related data and functionality inside classes.
Maintainability
Make larger applications easier to understand, update and maintain.
Summary
TypeScript OOP helps developers organize applications using classes and objects. Constructors initialize objects, inheritance supports code reuse, encapsulation controls access to data, access modifiers define visibility, abstract classes provide a common base, and static members belong to the class rather than an instance.
Next, learn how to create classes and objects in TypeScript with practical examples.