Learn the basics of TypeScript classes, their syntax, structure, and how they encapsulate data and behavior in object-oriented programming.
In this section, we delve into the world of classes in TypeScript, a powerful feature that allows us to create objects and encapsulate data and behavior. Understanding classes is a fundamental step in mastering object-oriented programming (OOP) in TypeScript. Let’s explore the syntax and structure of classes, how they compare to traditional OOP languages, and how they can be used to model real-world objects.
A class in TypeScript is a blueprint for creating objects. It defines properties and methods that the objects created from the class will have. Classes encapsulate data (properties) and behavior (methods) in a single unit, making it easier to manage and organize code.
Let’s start by looking at the basic structure of a class in TypeScript:
class Car {
// Properties
make: string;
model: string;
year: number;
// Constructor
constructor(make: string, model: string, year: number) {
this.make = make;
this.model = model;
this.year = year;
}
// Method
displayDetails(): void {
console.log(`Car: ${this.make} ${this.model}, Year: ${this.year}`);
}
}
// Creating an object of the Car class
const myCar = new Car('Toyota', 'Corolla', 2020);
myCar.displayDetails(); // Output: Car: Toyota Corolla, Year: 2020
Properties: These are variables that hold data specific to the class. In the Car
class, make
, model
, and year
are properties.
Constructor: A special method used to initialize objects. It is called when an object is created from the class. The constructor in the Car
class initializes the properties with values provided as arguments.
Methods: Functions defined inside a class that describe the behaviors of the objects. The displayDetails
method in the Car
class is an example of a method that prints the car’s details.
Encapsulation is one of the core principles of OOP. It refers to the bundling of data (properties) and methods (behavior) that operate on the data into a single unit, or class. This helps in protecting the data from outside interference and misuse.
In our Car
class example, encapsulation is demonstrated by having the properties and methods within the class. This makes it easy to manage the car’s data and behavior in one place.
TypeScript classes are similar to classes in traditional OOP languages like Java and C#. However, there are some differences:
public
, private
, and protected
, which control the visibility of properties and methods.Here’s an example of using access modifiers in TypeScript:
class BankAccount {
private balance: number;
constructor(initialBalance: number) {
this.balance = initialBalance;
}
public deposit(amount: number): void {
this.balance += amount;
}
public withdraw(amount: number): void {
if (amount <= this.balance) {
this.balance -= amount;
} else {
console.log('Insufficient funds');
}
}
public getBalance(): number {
return this.balance;
}
}
const myAccount = new BankAccount(1000);
myAccount.deposit(500);
console.log(myAccount.getBalance()); // Output: 1500
myAccount.withdraw(2000); // Output: Insufficient funds
In this example, the balance
property is private, meaning it cannot be accessed directly from outside the class. This is a key feature of encapsulation, ensuring that the balance can only be modified through the deposit
and withdraw
methods.
Classes are a great way to model real-world objects in your code. When designing a class, think about the properties and behaviors that the object should have.
For example, consider a Person
class:
class Person {
name: string;
age: number;
constructor(name: string, age: number) {
this.name = name;
this.age = age;
}
greet(): void {
console.log(`Hello, my name is ${this.name} and I am ${this.age} years old.`);
}
}
const person1 = new Person('Alice', 30);
person1.greet(); // Output: Hello, my name is Alice and I am 30 years old.
In this Person
class, name
and age
are properties, and greet
is a method that describes a behavior of the person.
Experiment with the examples provided by modifying the properties and methods. For instance, add a new method to the Car
class that calculates the age of the car based on the current year. This will help reinforce your understanding of classes and their structure.
To further aid your understanding, let’s visualize the structure of a class using a diagram:
classDiagram class Car { - make: string - model: string - year: number + Car(make: string, model: string, year: number) + displayDetails(): void }
This diagram represents the Car
class, showing its properties and methods. The -
symbol indicates private members, while the +
symbol indicates public members.
In this section, we’ve explored the basics of classes in TypeScript, including their syntax and structure. We’ve seen how classes encapsulate data and behavior, making it easier to manage code. By comparing TypeScript classes with those in traditional OOP languages, we highlighted the unique features TypeScript offers, such as type annotations and access modifiers. Finally, we encouraged you to think of real-world objects as classes, providing a solid foundation for modeling complex systems.