TS
JV

TypeScript to Java

10 lessons

Progress0%
1Introduction: Compiler to JVM2Type Systems: Structural vs Nominal3Classes & OOP4Generics5Modules to Packages6Null Safety7Async to Threads8Ecosystem9Exception Handling10Collections and Stream API
All Mirror Courses
TS
JV
Classes & OOP
MirrorLesson 3 of 10
Lesson 3

Classes & OOP

Classes & OOP

Introduction

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

Mirror Card
TS
From TypeScript:

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

JV
In Java:

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

The Java Way

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

JV
Java Example
abstract class Shape {
    protected String color;

    public Shape(String color) {
        this.color = color;
    }

    public abstract double area();

    public String describe() {
        return "A " + color + " shape with area " + area();
    }
}

class Circle extends Shape {
    private double radius;

    public Circle(String color, double radius) {
        super(color);
        this.radius = radius;
    }

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

// Usage:
Circle c = new Circle("red", 5);
System.out.println(c.describe());

Comparing to TypeScript

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

TS
TypeScript (What you know)
abstract class Shape {
  constructor(protected color: string) {}
  abstract area(): number;
  describe(): string {
    return `A ${this.color} shape with area ${this.area()}`;
  }
}

class Circle extends Shape {
  constructor(color: string, private radius: number) {
    super(color);
  }
  area(): number {
    return Math.PI * this.radius ** 2;
  }
}

const c = new Circle("red", 5);
console.log(c.describe());
Mirror Card
TS
From TypeScript:

You may be used to different syntax or behavior.

JV
In Java:

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

Mirror Card
TS
From TypeScript:

You may be used to different syntax or behavior.

JV
In Java:

abstract classes and methods are identical in concept

Mirror Card
TS
From TypeScript:

You may be used to different syntax or behavior.

JV
In Java:

Java requires explicit field declarations before the constructor

Mirror Card
TS
From TypeScript:

You may be used to different syntax or behavior.

JV
In Java:

No constructor parameter shorthand — must assign manually

Step-by-Step Breakdown

1. Abstract Classes Are Nearly Identical

Both TypeScript and Java support abstract classes with abstract methods. The keyword and semantics are the same.

TS
TypeScript
abstract class Animal {
  abstract speak(): string;
}
JV
Java
abstract class Animal {
    public abstract String speak();
}

2. No Constructor Shorthand in Java

TypeScript's constructor(private name: string) declares and assigns in one line. Java requires a field declaration and manual assignment.

TS
TypeScript
constructor(private name: string, public age: number) {}
JV
Java
private String name;
public int age;

public Person(String name, int age) {
    this.name = name;
    this.age = age;
}
Common Pitfall
Forgetting to declare fields above the constructor is a common Java beginner mistake.

3. Access Modifiers Are Enforced at Compile Time

Unlike TypeScript's modifiers which are erased at runtime, Java enforces private/protected/public in the compiled bytecode.

TS
TypeScript
private name: string; // erased in JS output
JV
Java
private String name; // enforced by JVM
Rule of Thumb
Default access in Java (no modifier) is package-private — visible within the same package.

4. final = readonly

Java's final keyword on a field means it cannot be reassigned after initialization — equivalent to TypeScript's readonly.

TS
TypeScript
readonly PI: number = 3.14159;
JV
Java
final double PI = 3.14159;

Common Mistakes

When coming from TypeScript, developers often make these mistakes:

  • Access modifiers (public, private, protected) work the same way
  • abstract classes and methods are identical in concept
  • Java requires explicit field declarations before the constructor
Common Pitfall
Don't assume Java works exactly like TypeScript. While the concepts may be similar, the syntax and behavior can differ significantly.

Key Takeaways

  • Abstract classes and access modifiers work the same way in both languages
  • Java requires explicit field declarations — no constructor parameter shorthand
  • Java enforces access modifiers at the bytecode level
  • final in Java is equivalent to readonly in TypeScript
Rule of Thumb
The best way to learn is by doing. Try rewriting some of your TypeScript code in Java to practice these concepts.
PreviousNext