Inheritance in Java: Types and Examples
Inheritance in Java lets one class reuse and extend another. A SavingsAccount can
inherit everything a BankAccount does and add interest on top, without copying any code. This guide
covers how inheritance works, the types of inheritance in Java, and why a class can't extend more than one parent.
What is inheritance in Java?
Inheritance creates an "is-a" relationship: a Dog is an Animal,
and a SavingsAccount is a BankAccount. The existing class is the
parent (also called superclass or base class). The new class is the
child (also called subclass or derived class).
You create inheritance with the extends keyword:
public class BankAccount {
protected double balance;
public void deposit(double amount) {
balance += amount;
}
}
public class SavingsAccount extends BankAccount {
private final double rate;
public SavingsAccount(double rate) {
this.rate = rate;
}
public void addInterest() {
balance += balance * rate; // uses the inherited field
}
} SavingsAccount s = new SavingsAccount(0.05);
s.deposit(200); // inherited from BankAccount
s.addInterest(); // defined in SavingsAccount
System.out.println(s.balance); 210.0
What does a child class get from its parent?
All non-private fields and methods. Private members still exist inside the object, but the child can only reach them through the parent's methods. Constructors are never inherited.
What is method overriding?
A child class can replace a parent's method with its own version. This is called overriding.
Mark it with @Override so the compiler checks you actually matched the parent's method:
public class Animal {
public String sound() {
return "...";
}
}
public class Dog extends Animal {
@Override
public String sound() {
return "Woof";
}
}
Animal pet = new Dog(); // a Dog stored in an Animal variable
System.out.println(pet.sound()); // Java calls Dog's version Woof
Java decides which version to run based on the actual object (a Dog), not the variable type
(Animal). That's polymorphism, and it's what makes inheritance powerful. To call
the parent's version from inside the child, use the super keyword.
What are the types of inheritance in Java?
There are five classic types. Java supports three of them directly with classes:
Explore the 5 types of inheritance
Single
One child extends one parent. The simplest and most common form. ✔ Supported with classes.
class Animal { }
class Dog extends Animal { } Multilevel
A chain: a grandchild extends a child, which extends a parent. ✔ Supported with classes.
class Animal { }
class Dog extends Animal { }
class Puppy extends Dog { } // Puppy is a Dog AND an Animal Hierarchical
Many children share one parent. ✔ Supported with classes.
class Animal { }
class Dog extends Animal { }
class Cat extends Animal { } Multiple
One child with several parents. ✘ Not allowed with classes. ✔ Allowed with interfaces.
class Duck extends Animal, Robot { } // ✘ compile error
class Duck implements Swimmer, Flyer { } // ✔ fine Hybrid
A mix of the above, such as hierarchical plus multiple. ✔ Possible only by combining one class with interfaces.
class Bird extends Animal implements Flyer { }
class Fish extends Animal implements Swimmer { }
class Duck extends Bird implements Swimmer { } | Type | Shape | With classes? | With interfaces? |
|---|---|---|---|
| Single | A → B | ✔ Yes | ✔ Yes |
| Multilevel | A → B → C | ✔ Yes | ✔ Yes |
| Hierarchical | A → B, A → C | ✔ Yes | ✔ Yes |
| Multiple | A + B → C | ✘ No | ✔ Yes |
| Hybrid | Mixture | ✘ No | ✔ Yes |
Why can't a Java class extend multiple classes?
Imagine Printer and Scanner both have a start() method, and
Copier extends Printer, Scanner. When you call copier.start(), which one should run? This
ambiguity is called the diamond problem. Java's designers avoided it by allowing only one parent class.
So how do I combine abilities from several sources?
Use interfaces. A class can implement as many as it needs. If two interfaces happen to provide the same default method, Java makes you override it and pick, so the ambiguity never sneaks in.
interface Printer { default String start() { return "printing"; } }
interface Scanner { default String start() { return "scanning"; } }
class Copier implements Printer, Scanner {
@Override
public String start() {
return Printer.super.start() + " + " + Scanner.super.start();
}
}
System.out.println(new Copier().start()); printing + scanning
Another option: composition. Instead of being a Printer, a Copier can have a Printer field and a Scanner field and delegate to them. Many experienced developers prefer this "has-a" style for flexibility.
Every class already inherits from Object
If a class doesn't say extends, Java quietly makes it extend java.lang.Object. That's
why every object has methods like toString(), equals(), and hashCode().
The hierarchy from our example
- java.lang.Object root of every class
- BankAccount extends Object (implicitly)
- SavingsAccount extends BankAccount
- BankAccount extends Object (implicitly)
A SavingsAccount is a BankAccount, and a BankAccount is an Object, so a SavingsAccount is all three.
How do you prevent inheritance?
finalclass — can't be extended at all, likeString. See the final keyword.finalmethod — can be inherited but not overridden.sealedclass (Java 17+) — only the subclasses you list are allowed:sealed class Shape permits Circle, Square.
Quick recap
extendscreates an "is-a" relationship; the child inherits non-private members.@Overridelets a child replace a parent method; Java picks the version by the real object type.- Classes support single, multilevel, and hierarchical inheritance, but only one parent class.
- Use interfaces (or composition) for multiple or hybrid inheritance.
- Every class ultimately extends
Object.
What's next?
Sometimes a parent class should never be created on its own, like a generic "Shape". That's what
abstract classes in Java are for. You may also want to revisit
access modifiers to see how protected fits in.
Frequently Asked Questions
Can a Java class extend multiple classes?
No. A Java class can extend only one other class. This avoids the 'diamond problem', where two parents define the same method and Java couldn't tell which one to use. To combine behaviour from several sources, implement multiple interfaces instead.
What are the types of inheritance in Java?
With classes, Java supports single, multilevel, and hierarchical inheritance. Multiple and hybrid inheritance aren't allowed between classes, but you can achieve them with interfaces, because a class can implement any number of interfaces.
Are private members inherited in Java?
A subclass object does contain the parent's private fields, but the subclass code can't access them directly. It has to use the parent's public or protected methods, such as getters, to read or change them.
Are constructors inherited in Java?
No. Constructors are never inherited. A subclass must define its own constructors. Each subclass constructor calls a parent constructor first, either explicitly with super(...) or implicitly with super() if you don't write it.
What is the root class of all Java classes?
java.lang.Object. Every class that doesn't explicitly extend another class automatically extends Object. That's why every object has methods like toString(), equals(), and hashCode().
How do I stop a class from being inherited?
Declare it final, for example 'public final class Money'. A final class can't be extended. Since Java 17 you can also use sealed classes to allow only a specific list of subclasses.