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.
In TypeScript, you're familiar with classes & oop.
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:
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:
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());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.
abstract classes and methods are identical in concept
You may be used to different syntax or behavior.
Java requires explicit field declarations before the constructor
You may be used to different syntax or behavior.
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.
abstract class Animal {
abstract speak(): string;
}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.
constructor(private name: string, public age: number) {}private String name;
public int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}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.
private name: string; // erased in JS outputprivate String name; // enforced by JVM4. final = readonly
Java's final keyword on a field means it cannot be reassigned after initialization — equivalent to TypeScript's readonly.
readonly PI: number = 3.14159;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
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