Classes and Object-Oriented Programming

Learn the core concepts of classes and object-oriented programming in TypeScript, and distinguish ES6 features from TypeScript additions.

🎯 Introduction

By the end of this article, you will understand how classes and object-oriented programming differ between ES6 and TypeScript, and how to write structured object-oriented TypeScript code. You will learn about class definitions, access modifiers, inheritance, implementation, abstract classes, and interfaces, including which features come from ES6 and which TypeScript adds.


✨ Class Definitions and Constructors

Before ES6, JavaScript primarily used prototype chains for object-oriented programming. ES6 introduced the class keyword, providing class syntax similar to traditional object-oriented languages. TypeScript extends ES6 classes with additional features and type checking.

// ES6 class definition and constructor
class Person {
    constructor(name) {
        this.name = name;
    }

    sayHello() {
        console.log('Hello, ' + this.name);
    }
}

const p = new Person('Hong');
p.sayHello();

The code above is supported by ES6. TypeScript uses the same class syntax, with optional type annotations to make the code safer:

// TypeScript class definition with types
class Person {
    name: string;

    constructor(name: string) {
        this.name = name;
    }

    sayHello(): void {
        console.log('Hello, ' + this.name);
    }
}

const p = new Person('Hong');
p.sayHello();
In TypeScript, class properties and methods can have type annotations to help catch errors at compile time.

🔐 Access Modifiers: public, private, protected

These access modifiers are TypeScript additions to classes. ES6 does not support them; its properties and methods are public by default.

  • public allows access from outside the class (the default in TypeScript)
  • private allows access only within the class
  • protected allows access within the class and its subclasses
class Person {
    public name: string; // Public property
    private age: number; // Private property
    protected gender: string; // Protected property

    constructor(name: string, age: number, gender: string) {
        this.name = name;
        this.age = age;
        this.gender = gender;
    }

    public sayHello(): void {
        console.log(`Hi, I'm ${this.name}`);
    }

    private getAge(): number {
        return this.age;
    }
}
These access modifiers are enforced during TypeScript type checking. They do not restrict access in the emitted JavaScript, but enforce access rules during development.

🧬 Inheritance and Implementation

ES6 supports inheritance through the extends keyword, which lets a subclass inherit from a base class.

// ES6 inheritance example
class Animal {
    constructor(name) {
        this.name = name;
    }
    move() {
        console.log(this.name + ' is moving');
    }
}

class Dog extends Animal {
    bark() {
        console.log('Woof');
    }
}

const dog = new Dog('Snow');
dog.move();
dog.bark();

TypeScript uses similar inheritance syntax, with additional type annotations:

class Animal {
    name: string;
    constructor(name: string) {
        this.name = name;
    }
    move(): void {
        console.log(this.name + ' is moving');
    }
}

class Dog extends Animal {
    bark(): void {
        console.log('Woof');
    }
}

const dog = new Dog('Snow');
dog.move();
dog.bark();

Implementing an Interface

Implementing an interface is a TypeScript concept; ES6 has no interfaces. An interface specifies the properties and methods a class must provide, and is checked statically.

interface Flyable {
    fly(): void;
}

class Bird implements Flyable {
    fly(): void {
        console.log('I can fly');
    }
}

TypeScript reports an error if a class does not implement all the required interface methods.

An interface acts as a contract for a class's structure, ensuring that it provides the expected capabilities.

🏗 Abstract Classes and Interfaces

Abstract Classes

Abstract classes are a TypeScript feature that ES6 does not support.

An abstract class cannot be instantiated directly. It typically serves as a base class and defines methods that subclasses must implement.

abstract class Animal {
    name: string;
    constructor(name: string) {
        this.name = name;
    }
    abstract makeSound(): void; // Abstract method: subclasses must implement it

    move(): void {
        console.log(this.name + ' is moving');
    }
}

class Cat extends Animal {
    makeSound(): void {
        console.log('Meow');
    }
}

const cat = new Cat('Patches');
cat.makeSound();

Interfaces

An interface describes a class's public shape without providing an implementation. A class can implement multiple interfaces.

interface Swimmer {
    swim(): void;
}

interface Flyer {
    fly(): void;
}

class Duck implements Swimmer, Flyer {
    swim(): void {
        console.log('The duck is swimming');
    }
    fly(): void {
        console.log('The duck is flying');
    }
}

❓ Questions and Answers

Q1: What is the main difference between ES6 classes and TypeScript classes?

ES6 provides basic class syntax and inheritance. TypeScript adds access modifiers, type annotations, interfaces, and abstract classes.

Q2: Do access modifiers apply in the compiled JavaScript?

These modifiers are enforced only during TypeScript type checking. They do not restrict access in the emitted JavaScript, but report invalid access during development.

Q3: How do interfaces differ from abstract classes?

Interfaces define contracts without implementations, and a class can implement multiple interfaces. Abstract classes can contain both implemented and abstract methods. They cannot be instantiated directly and are intended to be extended.


🧪 Practice Exercises

Exercise 1

Write an abstract class Vehicle with an abstract method move() and a concrete method stop(). Then write a Car class that extends Vehicle and implements move().

abstract class Vehicle {
    abstract move(): void;
    stop(): void {
        console.log('Stopped');
    }
}

// Write a Car class that extends Vehicle and implements move()

Exercise 2

Define an interface Playable with a play() method. Write a class Music that implements it and prints a message about playing music from its play() method.

interface Playable {
    play(): void;
}

// Write a Music class that implements Playable

🎉 You now understand TypeScript class definitions, access modifiers, inheritance, implementation, abstract classes, and interfaces! Object-oriented design helps you write more structured, maintainable code, and TypeScript's type system makes that process more reliable. Keep practicing to become more comfortable with these tools.