7.2 Inheritance
In Java, inheritance is a core OOP concept that lets you create new classes by reusing existing ones.
Just as children inherit assets from their parents in real life, a child (subclass) class in Java can inherit the fields and methods of a parent (superclass) class and use them as its own. This eliminates code duplication and greatly improves maintainability.
1. The extends Keyword and Inheritance Basics
To implement inheritance, write the extends keyword after the child class name followed by the name of the parent class you want to inherit from.
class ParentClass {
// parent's fields and methods
}
class ChildClass extends ParentClass {
// child's own unique fields and methods
// (and also has access to everything in parent)
}
- Parent Class (Superclass): The class that is inherited from. Also called superclass or base class.
- Child Class (Subclass): The class that inherits. Also called subclass or derived class.
Example: Inheritance Basics
Let's use an easy-to-understand example — game characters.
// Parent class: common attributes for all characters
class Character {
String name;
int hp;
void walk() {
System.out.println(name + " is walking. (stomp stomp)");
}
}
// Child class: Warrior inherits from Character
class Warrior extends Character {
int rage; // field unique to Warrior
void attack() {
System.out.println("Warrior " + name + " attacks with a sword! Clang!");
}
}
// Child class: Magician inherits from Character
class Magician extends Character {
int mp; // field unique to Magician
void castSpell() {
System.out.println("Magician " + name + " casts Fireball! Boom!");
}
}
public class InheritanceExample {
public static void main(String[] args) {
Warrior w = new Warrior();
w.name = "DarkWarrior"; // inherited from parent
w.hp = 1000; // inherited from parent
w.rage = 100; // own field
w.walk(); // parent method
w.attack(); // own method
Magician m = new Magician();
m.name = "Asiana";
m.hp = 500;
m.mp = 1500;
m.walk();
m.castSpell();
}
}
Notice that Warrior doesn't define name or walk() inside itself, yet it can use them thanks to extends Character. The code is concise, and if you later want "all characters should also be able to run (run())", you only need to modify Character in one place — very efficient.
2. Important Rules of Java Inheritance
- Single Inheritance Only:
- Unlike C++, Java forbids multiple class inheritance. A child class can only have one parent class.
class Child extends Father, Mother { }is not allowed. (This keeps relationships clear and avoids ambiguity.)
Objectis the Ultimate Ancestor of All Classes:- Every class you create in Java automatically inherits from
java.lang.Object, even if you don't write it explicitly. - The familiar methods
toString()andequals()are methods inherited fromObject.
- Every class you create in Java automatically inherits from
- Constructors Are Not Inherited:
- Member variables and methods are inherited, but constructors and initialization blocks are not. However, when a child class constructor is called, the parent class's default constructor is implicitly called first to help with initialization.
3. The super Keyword and super()
In a previous chapter you learned the this keyword to refer to the current instance. Similarly, inside a child class you use super to refer to the parent class.
super: A reference variable inside the child class that points to the inherited parent class members.super(): Calls the constructor of the parent class.
super() Constructor Call Example
When a child object is created, the parent portion must also be allocated in memory so the child can fully use the inherited properties. Java enforces this by requiring that the parent constructor super() be called on the very first line of any child constructor. (If you don't write it, the compiler automatically inserts super(); on the first line.)
class Parent {
int x;
Parent() {
System.out.println("Parent default constructor called!");
this.x = 10;
}
}
class Child extends Parent {
int y;
Child() {
// The compiler inserts super(); here automatically.
System.out.println("Child default constructor called!");
this.y = 20;
}
}
public class SuperExample {
public static void main(String[] args) {
Child c = new Child();
}
}
[Output]
Parent default constructor called!
Child default constructor called!
Java inheritance reduces the inconvenience of copy-pasting code and lets you manage well-written common logic in one place efficiently. In the next section, 7.3, you will learn how child classes can customize (override) inherited parent methods and how to harness polymorphism — the true power of inheritance.