Inheritance in Java lets one class reuse and extend the fields and methods of another. You declare the relationship with the extends keyword: class Dog extends Animal makes Dog a subclass of the superclass Animal, so a Dog automatically has everything accessible in Animal and can add or change behaviour.
This guide covers extends, constructor chaining with super, method overriding, the Object root class, and when to choose composition instead. Examples work on Java 21 and Java 25 LTS.
Inheritance models an is-a relationship: a dog is an animal, a car is a vehicle. The subclass inherits non-private members of the superclass, and Java allows a class to extend exactly one class.
Dog did not write eat(), but it has it. Private members exist in the object but are not directly accessible from the subclass. Constructors are never inherited.
When you create a Dog, the Animal part must be built first. Every constructor starts by calling a superclass constructor, either explicitly with super(...) or implicitly with super() if you write nothing.
If the superclass has no no-argument constructor and you do not call super(args), the code does not compile. Since Java 22 you may run some statements before super(...) in preview or newer language levels, but the plain rule above is what you should learn first.
The super keyword also reaches a parent method that you have overridden: super.eat().
A subclass can replace the behaviour of an inherited method by declaring a method with the same name and parameters. Add @Override so the compiler checks your intent.
The overriding method must have the same signature, a compatible return type and access that is not narrower than the original. You cannot override private, static or final methods. If you mark a class final, no one can extend it, as with String.
Java supports single inheritance (one parent), multilevel inheritance (A to B to C) and hierarchical inheritance (many children of one parent). A class cannot extend two classes, which avoids the ambiguity known as the diamond problem. To share behaviour from several sources, a class implements several interfaces.
Every class without an explicit extends clause inherits from java.lang.Object. That is why every object has toString(), equals() and hashCode(). A reference of the parent type can hold a child object:
Sealed classes (final since Java 17) let you restrict which classes may extend a parent with sealed and permits, useful when you want a closed hierarchy.
Inheritance couples the child tightly to the parent. A change in the superclass can break every subclass. Use it only when the is-a relationship is true and stable. If the relationship is has-a, hold the other object in a field instead.
| Question | Use inheritance | Use composition |
|---|---|---|
| Relationship | Dog is an Animal | Car has an Engine |
| Coupling | Tight | Loose |
| Swap behaviour at runtime | Hard | Easy |
Extending a class just to reuse a couple of methods is a common beginner mistake. A Stack that extends ArrayList exposes methods like add(index, x) that break the idea of a stack.
Not for classes. A class can extend only one class, but it can implement many interfaces, which gives most of the benefits without the diamond problem.
No. A subclass must declare its own constructors, and each one calls a superclass constructor with super(...), explicitly or implicitly.
Overloading means several methods with the same name and different parameters in one class. Overriding means a subclass redefines a method with the same signature as its parent.
Declare it final. In Java 17 and later you can also use sealed to allow only a named list of subclasses.
Inheritance with extends lets a subclass reuse and specialise a superclass, calling the parent constructor with super and replacing behaviour through overriding. Java allows single class inheritance, with Object at the top of every hierarchy. Use it for true is-a relationships and choose composition for has-a ones.