Inheritance: The "is-a" Relationship
In the real world, objects often share common traits. A Dog is an Animal. A Car is a Vehicle. In Java, Inheritance allows one class to "inherit" the fields and methods of another. This is the secret to writing clean code that isn't repetitive.
1. Key Terminology
- Superclass (Parent): The class that is being inherited from (the general category).
- Subclass (Child): The class that inherits from the parent (the specific version).
extends: The keyword used to create the link between the two.
2. Basic Inheritance in Action
When a child class extends a parent, it automatically gets everything the parent has (except for private members).
// Parent Class
class Animal {
String name;
void eat() {
System.out.println(name + " is eating...");
}
}
// Child Class
class Dog extends Animal {
void bark() {
System.out.println("Woof! Woof!");
}
}
Usage:
Dog myDog = new Dog();
myDog.name = "Buddy"; // Inherited from Animal
myDog.eat(); // Inherited from Animal
myDog.bark(); // Specific to Dog
3. Method Overriding
Sometimes, the child wants to do something differently than the parent. You can rewrite a parent's method in the child class—this is called Overriding.
We use the @Override annotation to tell Java (and ourselves) that we are intentionally changing a parent's behavior.
class Cat extends Animal {
@Override
void eat() {
System.out.println(name + " picks at the food carefully.");
}
}
4. The super Keyword
If you want to use something from the parent class inside the child class, you use the keyword super. This is most common in constructors.
class Animal {
String name;
Animal(String name) {
this.name = name;
}
void eat() {
System.out.println(name + " is eating...");
}
}
class Bird extends Animal {
boolean canFly;
Bird(String name, boolean canFly) {
super.name = name; // Accessing parent field
this.canFly = canFly;
}
}
💻 Full Practical Example: The Vehicle System
Copy this code to see how different subclasses share a common parent:
class Vehicle {
String brand;
void honk() {
System.out.println("Beep beep!");
}
}
class Car extends Vehicle {
int doors = 4;
}
class Motorcycle extends Vehicle {
@Override
void honk() {
System.out.println("Meep meep!"); // Motorcycles have a higher pitch!
}
}
public class Main {
public static void main(String[] args) {
Car myCar = new Car();
Motorcycle myBike = new Motorcycle();
myCar.brand = "Toyota";
myBike.brand = "Yamaha";
myCar.honk(); // Output: Beep beep!
myBike.honk(); // Output: Meep meep!
}
}
⚠️ Important Rules
- Single Inheritance: In Java, a class can only extend one parent class. You cannot have
class Dog extends Animal, Mammal. - Private is Private: A subclass cannot directly access
privatefields of the parent (unless you use Getters and Setters).
💡 Challenge: The Employee Hierarchy
- Create a parent class
Employeewith anameand a methodwork(). - Create a subclass
Developerthat overrideswork()to say "Writing code...". - Create another subclass
Managerthat overrideswork()to say "Leading meetings...". - In
main, create an array ofEmployeeobjects containing both a Developer and a Manager, and loop through them callingwork().