TypeScript Classes

TypeScript tutorial

🏛️ TypeScript Classes

In TypeScript classes are blueprints for creating objects.

They support Object-Oriented Programming (OOP) concepts like encapsulation, inheritance, and polymorphism, with strong typing.


1️⃣ Basic 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});
}
}const p1 = new Person(“Sanjit”, 25);
p1.greet();


2️⃣ Access Modifiers

TypeScript provides three access modifiers:

Modifier Access
public Accessible everywhere (default)
private Accessible only inside the class
protected Accessible inside class & subclasses
class User {
public username: string;
private password: string;
constructor(username: string, password: string) {
this.username = username;
this.password = password;
}checkPassword(): void {
console.log(“Password is protected”);
}
}

user.password → Error


3️⃣ Parameter Properties (Shortcut ⭐)

TypeScript allows defining and initializing properties directly in constructor.

class Employee {
constructor(
public id: number,
public name: string,
private salary: number
) {}
}

✔ Less code
✔ Cleaner syntax


4️⃣ Readonly Properties

Cannot be modified after initialization.

class Product {
readonly id: number;
constructor(id: number) {
this.id = id;
}
}

5️⃣ Inheritance (extends)

One class can inherit another.

class Animal {
move(): void {
console.log("Moving...");
}
}
class Dog extends Animal {
bark(): void {
console.log(“Barking…”);
}
}const d = new Dog();
d.move();
d.bark();

6️⃣ Method Overriding

class Shape {
area(): number {
return 0;
}
}
class Square extends Shape {
constructor(private side: number) {
super();
}area(): number {
return this.side * this.side;
}
}

7️⃣ super Keyword

Used to call parent class constructor or methods.

class Vehicle {
constructor(public brand: string) {}
}
class Car extends Vehicle {
constructor(brand: string, public model: string) {
super(brand);
}
}

8️⃣ Implementing Interfaces

Classes can implement interfaces.

interface Logger {
log(message: string): void;
}
class ConsoleLogger implements Logger {
log(message: string): void {
console.log(message);
}
}

9️⃣ Static Properties & Methods

Belong to the class itself, not instances.

class MathUtil {
static PI: number = 3.14;
static square(x: number): number {
return x * x;
}
}MathUtil.square(5);

🔟 Abstract Classes

Used as a base class and cannot be instantiated.

abstract class Bank {
abstract getRate(): number;
calculateInterest(amount: number): number {
return amount * this.getRate();
}
}class SBI extends Bank {
getRate(): number {
return 0.05;
}
}

🔑 Classes Summary

Feature Syntax
Class class A {}
Constructor constructor()
Inheritance extends
Interface implements
Access Control public / private / protected
Static static
Abstract abstract

⭐ Best Practices

✔ Use private/protected for encapsulation
✔ Prefer interfaces for contracts
✔ Use readonly for immutable data
✔ Keep classes small & focused

You may also like...