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.
In Java, you're familiar with classes & oop.
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:
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:
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());You may be used to different syntax or behavior.
Class syntax is nearly identical — implements, access modifiers, and method signatures all look the same
You may be used to different syntax or behavior.
TypeScript constructor shorthand: 'constructor(private name: string)' declares the field and assigns it in one line
You may be used to different syntax or behavior.
Access modifiers (public, private, protected) work the same way
You may be used to different syntax or behavior.
The implements keyword works the same, but TypeScript checks structurally, not nominally
You may be used to different syntax or behavior.
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.
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}class Person {
// Shorthand: modifier in constructor param
constructor(
private name: string,
private age: number
) {}
// Fields are automatically created and assigned
}2. Abstract Classes
TypeScript supports abstract classes with the same syntax as Java, but the enforcement is compile-time only.
abstract class Animal {
abstract String sound();
void breathe() { System.out.println("inhale"); }
}abstract class Animal {
abstract sound(): string;
breathe(): void { console.log("inhale"); }
}
class Cat extends Animal {
sound(): string { return "meow"; }
}
// new Animal(); // compile error — abstract3. Readonly and Static
TypeScript has readonly (like Java's final) and static members with the same keyword.
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.
public String getName() { return name; }
public void setName(String name) { this.name = name; }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 getterCommon 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
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