PY
JV

Python to Java

11 lessons

Progress0%
1Introduction2Variables & Types3Functions to Methods4Lists to Arrays5Dicts to Maps6Classes & OOP7Inheritance8Exception Handling9Modules to Packages10Ecosystem11Modern Java Features
All Mirror Courses
PY
JV
Classes & OOP
MirrorLesson 6 of 11
Lesson 6

Classes & OOP

Classes & OOP

Introduction

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

Mirror Card
PY
From Python:

In Python, 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
public class Person {
    private String name;   // private field
    private int age;

    // Constructor
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    // Getter (like @property)
    public String getName() { return name; }

    // Setter
    public void setAge(int age) { this.age = age; }

    @Override
    public String toString() {
        return "Person(" + name + ", " + age + ")";
    }
}

// Usage
Person p = new Person("Alice", 30);
System.out.println(p.getName());
System.out.println(p);

Comparing to Python

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

PY
Python (What you know)
class Person:
    def __init__(self, name, age):
        self.__name = name   # private
        self.age = age

    @property
    def name(self):
        return self.__name

    def __str__(self):
        return f"Person({self.__name}, {self.age})"


p = Person("Alice", 30)
print(p.name)
print(p)
Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

JV
In Java:

Python self is explicit in every method; Java uses this (usually implicit)

Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

JV
In Java:

Python __init__ → Java constructor (same name as the class)

Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

JV
In Java:

Python __str__ → Java @Override toString()

Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

JV
In Java:

Python @property → Java getters (getName()) and setters (setName())

Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

JV
In Java:

Java access modifiers (public/private/protected) are explicit; Python uses __ convention

Step-by-Step Breakdown

1. Constructor

Java's constructor has the same name as the class and no return type. this.name resolves the conflict between the field and parameter.

PY
Python
def __init__(self, name, age):
    self.name = name
    self.age = age
JV
Java
public Person(String name, int age) {
    this.name = name;
    this.age = age;
}

2. Access Modifiers

Java uses public/private keywords. Python uses double-underscore (__name) as a naming convention for private fields.

PY
Python
self.__name = name   # private by convention
JV
Java
private String name;  // enforced by compiler
Common Pitfall
Python name mangling (__name) is not true privacy — it is just renamed to _ClassName__name. Java private is compiler-enforced.

3. Properties vs Getters

Python @property lets you access a method like an attribute. Java uses explicit getter methods named getX().

PY
Python
@property
def name(self):
    return self.__name
# used as: p.name
JV
Java
public String getName() { return name; }
// used as: p.getName()

4. toString (like __str__)

@Override toString() is called automatically when the object is printed or concatenated — just like Python's __str__.

PY
Python
def __str__(self):
    return f"Person({self.name})"
JV
Java
@Override
public String toString() {
    return "Person(" + name + ")";
}

Common Mistakes

When coming from Python, developers often make these mistakes:

  • Python self is explicit in every method; Java uses this (usually implicit)
  • Python __init__ → Java constructor (same name as the class)
  • Python __str__ → Java @Override toString()
Common Pitfall
Don't assume Java works exactly like Python. While the concepts may be similar, the syntax and behavior can differ significantly.

Key Takeaways

  • __init__ → constructor, self → this
  • __str__ → @Override toString()
  • @property → getX() / setX() getter-setter pattern
Rule of Thumb
The best way to learn is by doing. Try rewriting some of your Python code in Java to practice these concepts.
PreviousNext