JV
TS

Java to TypeScript

10 lessons

Progress0%
1Introduction to TypeScript2Type Systems3Classes & OOP4Generics5Modules & Packages6Null Safety7Async vs Threads8Ecosystem9Advanced TypeScript Types10Build Tooling
All Mirror Courses
JV
TS
Classes & OOP
MirrorLesson 3 of 10
Lesson 3

Classes & OOP

Classes & OOP

Introduction

In this lesson, you'll learn about classes & oop in TypeScript. Coming from Java, you already have a foundation for understanding this concept. We'll build on that knowledge while highlighting the key differences.

Mirror Card
JV
From Java:

In Java, you're familiar with classes & oop.

TS
In TypeScript:

TypeScript has its own approach to classes & oop, which we'll explore step by step.

The TypeScript Way

Let's see how TypeScript handles this concept. Here's a typical example:

TS
TypeScript Example
interface Shape {
    area(): number;
}

class Circle implements Shape {
    // Constructor shorthand — declares AND assigns
    constructor(private readonly radius: number) {}

    area(): number {
        return Math.PI * this.radius * this.radius;
    }

    getRadius(): number { return this.radius; }
}

const c = new Circle(5.0);
console.log(c.area());

Comparing to Java

Here's how you might have written similar code in Java:

JV
Java (What you know)
public interface Shape {
    double area();
}

public class Circle implements Shape {
    private final double radius;

    public Circle(double radius) {
        this.radius = radius;
    }

    @Override
    public double area() {
        return Math.PI * radius * radius;
    }

    public double getRadius() { return radius; }
}

Circle c = new Circle(5.0);
System.out.println(c.area());
Mirror Card
JV
From Java:

You may be used to different syntax or behavior.

TS
In TypeScript:

Class syntax is nearly identical — implements, access modifiers, and method signatures all look the same

Mirror Card
JV
From Java:

You may be used to different syntax or behavior.

TS
In TypeScript:

TypeScript constructor shorthand: 'constructor(private name: string)' declares the field and assigns it in one line

Mirror Card
JV
From Java:

You may be used to different syntax or behavior.

TS
In TypeScript:

Access modifiers (public, private, protected) work the same way

Mirror Card
JV
From Java:

You may be used to different syntax or behavior.

TS
In TypeScript:

The implements keyword works the same, but TypeScript checks structurally, not nominally

Mirror Card
JV
From Java:

You may be used to different syntax or behavior.

TS
In TypeScript:

Abstract class enforcement in TypeScript is compile-time only — no runtime check like Java's JVM

Step-by-Step Breakdown

1. Constructor Shorthand

TypeScript lets you declare and initialize fields directly in the constructor parameter list, eliminating boilerplate.

JV
Java
private String name;
private int age;

public Person(String name, int age) {
    this.name = name;
    this.age = age;
}
TS
TypeScript
class Person {
    // Shorthand: modifier in constructor param
    constructor(
        private name: string,
        private age: number
    ) {}
    // Fields are automatically created and assigned
}
Rule of Thumb
Use constructor shorthand for simple field assignments. Use explicit fields only when you need extra initialization logic.

2. Abstract Classes

TypeScript supports abstract classes with the same syntax as Java, but the enforcement is compile-time only.

JV
Java
abstract class Animal {
    abstract String sound();
    void breathe() { System.out.println("inhale"); }
}
TS
TypeScript
abstract class Animal {
    abstract sound(): string;
    breathe(): void { console.log("inhale"); }
}

class Cat extends Animal {
    sound(): string { return "meow"; }
}

// new Animal(); // compile error — abstract

3. Readonly and Static

TypeScript has readonly (like Java's final) and static members with the same keyword.

TS
TypeScript
class Config {
    static readonly MAX_RETRIES = 3; // like Java static final
    readonly createdAt: Date;

    constructor() {
        this.createdAt = new Date();
    }
}

console.log(Config.MAX_RETRIES); // 3
// Config.MAX_RETRIES = 5; // compile error!

4. Getters and Setters

TypeScript has built-in get/set syntax that looks cleaner than Java's explicit getter/setter methods.

JV
Java
public String getName() { return name; }
public void setName(String name) { this.name = name; }
TS
TypeScript
class Person {
    private _name: string = "";

    get name(): string {
        return this._name;
    }

    set name(value: string) {
        if (value.length === 0) throw new Error("Name required");
        this._name = value;
    }
}

const p = new Person();
p.name = "Alice";       // calls setter
console.log(p.name);    // calls getter

Common Mistakes

When coming from Java, developers often make these mistakes:

  • Class syntax is nearly identical — implements, access modifiers, and method signatures all look the same
  • TypeScript constructor shorthand: 'constructor(private name: string)' declares the field and assigns it in one line
  • Access modifiers (public, private, protected) work the same way
Common Pitfall
Don't assume TypeScript works exactly like Java. While the concepts may be similar, the syntax and behavior can differ significantly.

Key Takeaways

  • Constructor shorthand (private name: string) eliminates field-declaration boilerplate
  • Access modifiers, implements, and abstract all work the same as Java
  • readonly is TypeScript's equivalent of Java's final
  • Built-in get/set syntax replaces verbose getX/setX methods
Rule of Thumb
The best way to learn is by doing. Try rewriting some of your Java code in TypeScript to practice these concepts.
PreviousNext