Java Inheritance - Complete Tutorial
Master Java Inheritance: Learn single, multilevel, hierarchical inheritance, super keyword, method overriding, constructors in inheritance, access modifiers, and real-world examples.
1. Introduction to Inheritance
Inheritance lets a subclass acquire fields and methods from a superclass. It models IS-A relationships and promotes code reuse.
- extends keyword links child to parent
- Single inheritance for classes in Java
- Child inherits accessible members
- Constructors are not inherited
extends inherits members; models IS-A; use super for parent constructor or method.
class Vehicle {
void start() { System.out.println("Engine on"); }
}
class Bike extends Vehicle {
void ringBell() { System.out.println("Ring!"); }
}
2. Types of Inheritance
Java supports several inheritance patterns for classes. Classes use extends (single parent only). Multiple inheritance of classes is not allowed — use interfaces instead.
| Type | Structure | Supported in Java (classes)? |
|---|---|---|
| Single | One child → one parent | Yes |
| Multilevel | Chain: C extends B extends A | Yes |
| Hierarchical | Many children → one parent | Yes |
| Multiple | One child → many parents | No (classes); Yes (interfaces) |
| Hybrid | Mix of above (e.g. multilevel + hierarchical) | Yes (as combination) |
1. Single Inheritance
One subclass extends exactly one superclass.
class Employee {
double baseSalary = 30000;
void work() { System.out.println("Employee working"); }
}
class Developer extends Employee { // one parent only
void code() { System.out.println("Writing Java code"); }
}
public class SingleInheritanceDemo {
public static void main(String[] args) {
Developer dev = new Developer();
dev.work();
dev.code();
}
}
2. Multilevel Inheritance
A class extends another subclass, forming a chain.
class LivingBeing {
void breathe() { System.out.println("Breathing..."); }
}
class Animal extends LivingBeing {
void move() { System.out.println("Moving..."); }
}
class Dog extends Animal {
void bark() { System.out.println("Woof!"); }
}
public class MultilevelDemo {
public static void main(String[] args) {
Dog dog = new Dog();
dog.breathe();
dog.move();
dog.bark();
}
}
3. Hierarchical Inheritance
Multiple subclasses share the same parent class.
class Vehicle {
void start() { System.out.println("Vehicle started"); }
}
class Car extends Vehicle {
void openTrunk() { System.out.println("Trunk opened"); }
}
class Bike extends Vehicle {
void ringBell() { System.out.println("Ring ring!"); }
}
public class HierarchicalDemo {
public static void main(String[] args) {
Car car = new Car();
Bike bike = new Bike();
car.start();
bike.start();
}
}
4. Multiple Inheritance (via Interfaces)
Java classes cannot extend two classes. A class can implements many interfaces.
interface Printable { void print(); }
interface Scannable { void scan(); }
class OfficeDevice implements Printable, Scannable {
public void print() { System.out.println("Printing document"); }
public void scan() { System.out.println("Scanning document"); }
}
public class MultipleDemo {
public static void main(String[] args) {
OfficeDevice printer = new OfficeDevice();
printer.print();
printer.scan();
}
}
5. Hybrid Inheritance
Combination of types — e.g. one base class with several subclasses, and one subclass extended further (multilevel + hierarchical).
class Shape {
void draw() { System.out.println("Drawing shape"); }
}
class Polygon extends Shape { } // hierarchical branch
class Circle extends Shape { } // hierarchical branch
class Rectangle extends Polygon { // multilevel under Polygon
void area() { System.out.println("Rectangle area"); }
}
3. Real-Life Examples — Which Inheritance Type?
| Real-life scenario | Inheritance type | Why |
|---|---|---|
SavingsAccount extends BankAccount |
Single | One specialized account type from one base account |
Manager → Employee → Person |
Multilevel | Person → employee traits → manager traits in layers |
Car, Truck, Bus extend Vehicle |
Hierarchical | Many vehicle types share common vehicle behavior |
Smartphone implements Camera, GPS, MusicPlayer |
Multiple (interfaces) | One device, many independent capabilities |
University: Person → Student/Teacher → GraduateStudent |
Hybrid | Hierarchical roles + multilevel specialization |
GUI: JButton, JLabel extend JComponent |
Hierarchical | Many widgets share Swing component behavior |
Animals: Mammal → Dog, Cat under Animal |
Hybrid | Shared animal + branched species types |
4. Visibility Modes (Access Modifiers)
Modifiers control who can access fields and methods. In inheritance, protected is especially important — it allows subclasses to use parent members.
| Modifier | Same class | Same package (non-subclass) | Subclass (same package) | Subclass (different package) | Different package (non-subclass) |
|---|---|---|---|---|---|
private |
Yes | No | No | No | No |
default (no modifier) |
Yes | Yes | Yes | No | No |
protected |
Yes | Yes | Yes | Yes | No |
public |
Yes | Yes | Yes | Yes | Yes |
| Modifier | Visibility scope | Typical use in inheritance |
|---|---|---|
private |
Only inside declaring class | Hide implementation; not inherited for direct use by child |
default |
Same package only | Package-level helpers; child in other package cannot access |
protected |
Same package + subclasses anywhere | Share with children but hide from unrelated classes |
public |
Everywhere | Public API of parent available to all |
class Parent {
private int secret = 1;
int pkgField = 2;
protected int family = 3;
public int open = 4;
}
class Child extends Parent {
void test() {
// secret; // error — private
pkgField = 10; // OK if same package
family = 20; // OK — protected
open = 30; // OK — public
}
}
5. The super Keyword
super refers to the immediate parent class. Use it to call the parent constructor or access overridden parent methods and hidden fields.
super()calls parent constructor — must be first line in constructorsuper.method()calls parent version of methodsuper.fieldaccesses parent field if shadowed
class Parent {
Parent() { System.out.println("Parent constructor"); }
}
class Child extends Parent {
Child() {
super();
System.out.println("Child constructor");
}
}
6. Constructors in Inheritance
Subclass constructors must initialize the parent part. If super() is omitted, the compiler inserts a call to the parent's no-arg constructor.
- Parent constructor runs before child body
super(args)passes arguments to parent- Compile error if parent has no matching constructor
class Person {
String name;
Person(String name) { this.name = name; }
}
class Student extends Person {
int id;
Student(String name, int id) {
super(name);
this.id = id;
}
}
7. Method Overriding in Inheritance
Subclasses override inherited methods to provide specialized behavior while keeping the same API as the parent.
- Same method signature required
- Use
@Override - Cannot override
finalmethods - Access modifier cannot be more restrictive
class Bird {
void fly() { System.out.println("Flying"); }
}
class Penguin extends Bird {
@Override
void fly() { System.out.println("Cannot fly"); }
}
8. Summary Points
- Inheritance models IS-A relationships and reuses code via
extends. - Java classes support single inheritance only; use interfaces for multiple inheritance of type.
- Single: one parent — e.g.
Developer extends Employee. - Multilevel: chain — e.g.
Dog → Animal → LivingBeing. - Hierarchical: many children, one parent — e.g.
CarandBikeextendVehicle. - Multiple (interfaces):
implements A, Bfor several contracts. - Hybrid: mix of multilevel and hierarchical in real apps.
- private — class only; default — package; protected — package + subclasses; public — everywhere.
super()must be first in constructor; parent initializes before child.- Override with same signature; cannot weaken access; use
@Override. - Prefer inheritance for true IS-A; use composition when reuse without IS-A.
Quick recall
extends = one class parent. implements = many interfaces. protected = child-friendly visibility. super = talk to parent.