OOP with TypeScript: Class, Inheritance and Access Modifiers
OOP with TypeScript: Class, Inheritance and Access Modifiers
TypeScript, as a type-safe superset of JavaScript, provides object-oriented programming (OOP) concepts, enabling developers to write more structured and error-resistant code. Classes, one of the main components of OOP, help us establish object-oriented architectures more easily. In this article, we will examine how to create classes, how to use inheritance, and the role of access modifiers using TypeScript.
Classes
In TypeScript, classes are structures that bring together the properties and behaviors of an object. With class definitions, objects (instances) can be created. Below you can find an example of a simple class definition:
class Car {
brand: string;
model: string;
constructor(brand: string, model: string) {
this.brand = brand;
this.model = model;
}
giveInfo(): void {
console.log(`Car Brand: ${this.brand}, Model: ${this.model}`);
}
}
const car1 = new Car("Toyota", "Corolla");
car1.giveInfo(); // Car Brand: Toyota, Model: Corolla
Inheritance
Inheritance allows a class (subclass) to inherit properties and methods from another class (superclass). This increases code reusability. Below, you can see an example of an electric vehicle class that inherits from a vehicle class:
class ElectricCar extends Car {
batteryCapacity: number;
constructor(brand: string, model: string, batteryCapacity: number) {
super(brand, model);
this.batteryCapacity = batteryCapacity;
}
giveInfo(): void {
super.giveInfo();
console.log(`Battery Capacity: ${this.batteryCapacity} kWh`);
}
}
const car2 = new ElectricCar("Tesla", "Model S", 100);
car2.giveInfo(); // Car Brand: Tesla, Model: Model S, Battery Capacity: 100 kWh
Access Modifiers
In TypeScript, access modifiers control the accessibility of class members (properties and methods). There are mainly three access modifiers: public, private, and protected. The public access modifier allows access from anywhere in the class, private allows access only within the class, and protected allows access within the class and its subclasses.
class Person {
private name: string;
protected age: number;
public profession: string;
constructor(name: string, age: number, profession: string) {
this.name = name;
this.age = age;
this.profession = profession;
}
public showInfo(): void {
console.log(`Name: ${this.name}, Age: ${this.age}, Profession: ${this.profession}`);
}
}
const person1 = new Person("Ahmet", 30, "Engineer");
person1.showInfo(); // Name: Ahmet, Age: 30, Profession: Engineer
In conclusion, object-oriented programming (OOP) concepts with TypeScript provide great flexibility and structure in software development. Thanks to features like classes, inheritance, and access modifiers, it is possible to write more reusable and maintainable code. TypeScript offers a strong foundation for developers, making it easier to manage complex applications. A deep understanding of OOP concepts is a major advantage in software engineering.

Yorum Gönder