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.
In Python, 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:
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:
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)You may be used to different syntax or behavior.
Python self is explicit in every method; Java uses this (usually implicit)
You may be used to different syntax or behavior.
Python __init__ → Java constructor (same name as the class)
You may be used to different syntax or behavior.
Python __str__ → Java @Override toString()
You may be used to different syntax or behavior.
Python @property → Java getters (getName()) and setters (setName())
You may be used to different syntax or behavior.
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.
def __init__(self, name, age):
self.name = name
self.age = agepublic 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.
self.__name = name # private by conventionprivate String name; // enforced by compiler3. Properties vs Getters
Python @property lets you access a method like an attribute. Java uses explicit getter methods named getX().
@property
def name(self):
return self.__name
# used as: p.namepublic 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__.
def __str__(self):
return f"Person({self.name})"@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()
Key Takeaways
- __init__ → constructor, self → this
- __str__ → @Override toString()
- @property → getX() / setX() getter-setter pattern