JS
JV

JavaScript to Java

10 lessons

Progress0%
1Variables & Types2Functions & Methods3Arrays & Collections4Classes & OOP5Exception Handling6Async vs Threads7Generics8String Methods9Interfaces and Abstract Classes10Build Tools and Ecosystem
All Mirror Courses
JS
JV
Classes & OOP
MirrorLesson 4 of 10
Lesson 4

Classes & OOP

Object-oriented programming

Introduction

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

Mirror Card
JS
From JavaScript:

In JavaScript, you're familiar with object-oriented programming.

JV
In Java:

Java has its own approach to object-oriented programming, 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
public abstract class Animal {
  private String name; // private field

  public Animal(String name) {
    this.name = name;
  }

  public String getName() { return name; } // getter

  public abstract String speak(); // must override
}

public class Dog extends Animal {
  public Dog(String name) { super(name); }

  @Override
  public String speak() {
    return getName() + " barks.";
  }
}

Dog d = new Dog("Rex");
System.out.println(d.speak());

Comparing to JavaScript

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

JS
JavaScript (What you know)
class Animal {
  #name; // private field

  constructor(name) {
    this.#name = name;
  }

  get name() { return this.#name; }

  speak() {
    return this.#name + " makes a sound.";
  }
}

class Dog extends Animal {
  constructor(name) { super(name); }

  speak() {
    return this.name + " barks.";
  }
}

const d = new Dog("Rex");
console.log(d.speak());
Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

JV
In Java:

Java uses public/private/protected keywords; JS uses # for private

Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

JV
In Java:

Java abstract classes cannot be instantiated; JS has no abstract keyword

Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

JV
In Java:

Java uses getter methods by convention; JS has get/set syntax

Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

JV
In Java:

@Override annotation documents intent; JS has no equivalent

Step-by-Step Breakdown

1. Access Modifiers

Java uses explicit public/private/protected keywords for access control. JS uses # for private fields.

JS
JavaScript
#name; // JS private
JV
Java
private String name; // Java private

2. Abstract Classes

Java abstract classes define a contract that subclasses must fulfill. In JS you'd throw an error in the base method.

JV
Java
public abstract String speak(); // subclass MUST implement

3. @Override

The @Override annotation tells the compiler to verify this method actually overrides a parent method — catching typos.

JS
JavaScript
speak() { return "barks"; } // no check in JS
JV
Java
@Override
public String speak() { return getName() + " barks."; }

Common Mistakes

When coming from JavaScript, developers often make these mistakes:

  • Java uses public/private/protected keywords; JS uses # for private
  • Java abstract classes cannot be instantiated; JS has no abstract keyword
  • Java uses getter methods by convention; JS has get/set syntax
Common Pitfall
Don't assume Java works exactly like JavaScript. While the concepts may be similar, the syntax and behavior can differ significantly.

Key Takeaways

  • JS # private → Java private keyword
  • Java abstract enforces method implementation
  • @Override documents and verifies overrides
  • Java getters are methods by convention (getName)
Rule of Thumb
The best way to learn is by doing. Try rewriting some of your JavaScript code in Java to practice these concepts.
PreviousNext