JV
PY

Java to Python

10 lessons

Progress0%
1Variables & Types2Classes & OOP3Collections4Exception Handling5File I/O6Functional Programming7Duck Typing and Protocols8Python Ecosystem9Type Hints and Static Analysis10Context Managers and Resources
All Mirror Courses
JV
PY
Classes & OOP
MirrorLesson 2 of 10
Lesson 2

Classes & OOP

Object-oriented programming

Introduction

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

Mirror Card
JV
From Java:

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

PY
In Python:

Python has its own approach to object-oriented programming, which we'll explore step by step.

The Python Way

Let's see how Python handles this concept. Here's a typical example:

PY
Python Example
from abc import ABC, abstractmethod

class Animal(ABC):
    def __init__(self, name: str):
        self._name = name

    @property
    def name(self) -> str:
        return self._name

    @abstractmethod
    def speak(self) -> str:
        pass

class Dog(Animal):
    def speak(self) -> str:
        return f"{self.name} barks."

d = Dog("Rex")
print(d.speak())
print(isinstance(d, Animal))

Comparing to Java

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

JV
Java (What you know)
public abstract class Animal {
    private String name;

    public Animal(String name) { this.name = name; }
    public String getName() { return name; }
    public abstract String speak();
}

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());
System.out.println(d instanceof Animal);
Mirror Card
JV
From Java:

You may be used to different syntax or behavior.

PY
In Python:

Python uses parentheses for inheritance; Java uses extends

Mirror Card
JV
From Java:

You may be used to different syntax or behavior.

PY
In Python:

Python @abstractmethod replaces Java's abstract keyword

Mirror Card
JV
From Java:

You may be used to different syntax or behavior.

PY
In Python:

Python @property replaces Java's getters/setters

Mirror Card
JV
From Java:

You may be used to different syntax or behavior.

PY
In Python:

Python __init__ replaces Java constructors

Step-by-Step Breakdown

1. Inheritance Syntax

Python inherits via class Dog(Animal). super().__init__() calls the parent constructor.

JV
Java
class Dog extends Animal { public Dog(String n) { super(n); } }
PY
Python
class Dog(Animal):
    def __init__(self, name: str):
        super().__init__(name)

2. @property vs Getters

Python @property replaces Java's getMethod/setMethod pattern with clean attribute-style access.

JV
Java
public String getName() { return name; }
PY
Python
@property
def name(self) -> str:
    return self._name

3. Abstract Methods

Python uses ABC (Abstract Base Class) and @abstractmethod decorator to enforce interface contracts.

JV
Java
public abstract String speak();
PY
Python
@abstractmethod
def speak(self) -> str:
    pass

Common Mistakes

When coming from Java, developers often make these mistakes:

  • Python uses parentheses for inheritance; Java uses extends
  • Python @abstractmethod replaces Java's abstract keyword
  • Python @property replaces Java's getters/setters
Common Pitfall
Don't assume Python works exactly like Java. While the concepts may be similar, the syntax and behavior can differ significantly.

Key Takeaways

  • extends → class Dog(Animal):
  • Java getters → @property decorator
  • abstract → ABC + @abstractmethod
  • __init__ replaces Java constructor
Rule of Thumb
The best way to learn is by doing. Try rewriting some of your Java code in Python to practice these concepts.
PreviousNext