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.
In Python, you're familiar with inheritance.
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:
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:
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):
passYou may be used to different syntax or behavior.
Python class Dog(Animal) → Java class Dog extends Animal
You may be used to different syntax or behavior.
Python allows multiple inheritance; Java allows only one parent class
You may be used to different syntax or behavior.
Java uses interface + implements for multiple type contracts
You may be used to different syntax or behavior.
@Override annotation makes overrides explicit and catches typos
You may be used to different syntax or behavior.
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.
class Dog(Animal):
def __init__(self, name):
super().__init__(name)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.
# Python: just define the same method name@Override
public String speak() {
return name + " barks";
}3. Interfaces for Multiple Types
Where Python uses multiple inheritance, Java uses interfaces — contracts that declare method signatures without implementation.
class RoboDog(Dog, Robot):
pass # multiple inheritanceclass 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.
self._name # convention: subclass-accessibleprotected String name; // compiler-enforced subclass accessCommon 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
Key Takeaways
- extends for inheritance; implements for interfaces
- Java allows only single class inheritance
- @Override makes method overriding explicit and safe