TypeScript Classes and Objects
Learn how to create classes, define properties and methods, create objects, and access class members in TypeScript.
1. What is a Class?
A class is a template used to create objects. It defines what data an object can store and what actions it can perform.
In TypeScript, we use the class keyword to create a class.
Properties store data, while methods define functionality.
Example: Student Class
class Student {
name: string = "Rahul";
age: number = 22;
displayInfo(): void {
console.log(this.name);
console.log(this.age);
}
}
In this example, Student is the class,
name and age are properties,
and displayInfo() is a method.
2. What is an Object?
An object is an instance of a class.
We create an object using the new keyword.
After creating an object, we can access its members using the dot (.) operator.
Example: Creating an Object
class Student {
name: string = "Rahul";
age: number = 22;
displayInfo(): void {
console.log(this.name);
console.log(this.age);
}
}
const student1 = new Student();
student1.displayInfo();
Expected Output
Rahul
22
new Student() creates a Student object,
and student1.displayInfo() calls its method.
3. Classes and Objects Workflow
Follow this diagram to understand how a class is used to create an object and how the object accesses properties and methods.
Define Class
Create a class named Student with properties and methods.
Create Object
Use new Student() to create an instance.
Access Properties
Read or update accessible data using the object.
Call Methods
Call methods to perform actions.
4. Properties and Methods
A class can contain properties to store information and methods to perform actions.
The this keyword refers to the current object instance.
Example
class Employee {
name: string = "Amit";
salary: number = 30000;
showDetails(): void {
console.log("Name: " + this.name);
console.log("Salary: " + this.salary);
}
}
const emp1 = new Employee();
emp1.showDetails();
Expected Output
Name: Amit
Salary: 30000
Properties
name and salary store employee information.
Method
showDetails() displays the employee information.
5. Creating Multiple Objects
A single class can create multiple objects. Each object can have its own property values.
Example
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);
const student2 = new Student("Priya", 21);
student1.displayInfo();
student2.displayInfo();
Expected Output
Rahul - 22
Priya - 21
Visual Example
Student Class
Common structure for student objects.
student1
Name: Rahul
Age: 22
student2
Name: Priya
Age: 21
6. Class vs Object
| Class | Object |
|---|---|
| A blueprint for creating objects. | An instance created from a class. |
| Defines properties and methods. | Uses the class-defined properties and methods. |
Declared using the class keyword. |
Usually created using the new keyword. |
Example: class Student { } |
Example: const s1 = new Student(); |
Summary
A class defines the structure of objects in TypeScript.
Objects are created using the new keyword.
Properties store data, methods perform actions, and the
this keyword refers to the current object instance.
One class can create multiple objects with different values.
Next, learn how constructors initialize objects when they are created.