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
Inheritance
MirrorLesson 7 of 11
Lesson 7

Inheritance

Inheritance

Introduction

In this lesson, you'll learn about inheritance 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 inheritance.

JV
In Java:

Java has its own approach to inheritance, 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 Animal {
    protected String name;

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

    public String speak() {
        return name + " makes a sound";
    }
}

public class Dog extends Animal {
    public Dog(String name) {
        super(name);   // call parent constructor
    }

    @Override
    public String speak() {   // override
        return name + " barks";
    }
}

// Java: single class inheritance only
// Use interfaces for multiple "types"
public interface Runnable {
    void run();
}

public class RaceDog extends Dog implements Runnable {
    public RaceDog(String name) { super(name); }
    public void run() { System.out.println(name + " runs!"); }
}

Comparing to Python

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

PY
Python (What you know)
class Animal:
    def __init__(self, name):
        self.name = name

    def speak(self):
        return f"{self.name} makes a sound"


class Dog(Animal):
    def __init__(self, name):
        super().__init__(name)

    def speak(self):          # override
        return f"{self.name} barks"


# Python supports multiple inheritance
class Robot:
    def beep(self): return "beep"

class RoboDog(Dog, Robot):
    pass
Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

JV
In Java:

Python class Dog(Animal) → Java class Dog extends Animal

Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

JV
In Java:

Python allows multiple inheritance; Java allows only one parent class

Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

JV
In Java:

Java uses interface + implements for multiple type contracts

Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

JV
In Java:

@Override annotation makes overrides explicit and catches typos

Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

JV
In Java:

super() works the same; Java requires super(args) in the constructor body

Step-by-Step Breakdown

1. extends vs (Parent)

Java uses the extends keyword. Python puts the parent class in parentheses.

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

2. @Override Annotation

@Override tells the compiler you intend to override a parent method. If the signature doesn't match, you get a compile error instead of silently creating a new method.

PY
Python
# Python: just define the same method name
JV
Java
@Override
public String speak() {
    return name + " barks";
}
Common Pitfall
Without @Override, a typo in the method name creates a new method and the parent's version is called instead — a very subtle bug.

3. Interfaces for Multiple Types

Where Python uses multiple inheritance, Java uses interfaces — contracts that declare method signatures without implementation.

PY
Python
class RoboDog(Dog, Robot):
    pass  # multiple inheritance
JV
Java
class RaceDog extends Dog implements Runnable {
    public void run() { /* required by interface */ }
}

4. protected Fields

Java protected makes a field accessible to subclasses. Python conventionally uses a single underscore (_name) for the same intent.

PY
Python
self._name  # convention: subclass-accessible
JV
Java
protected String name;  // compiler-enforced subclass access

Common Mistakes

When coming from Python, developers often make these mistakes:

  • Python class Dog(Animal) → Java class Dog extends Animal
  • Python allows multiple inheritance; Java allows only one parent class
  • Java uses interface + implements for multiple type contracts
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

  • extends for inheritance; implements for interfaces
  • Java allows only single class inheritance
  • @Override makes method overriding explicit and safe
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