Skip to content

Inheritance in Java: Types, Rules & Practical Examples

Inheritance can reduce duplication when there is a genuine IS-A relationship, but composition is often better when classes only need to reuse behaviour.

Practical Guide Student Focused Updated September 2026

How Java Inheritance Works

A subclass uses extends to inherit accessible members from a superclass. It can add new behaviour and override inherited methods while preserving the parent-child relationship.

Common Inheritance Forms in Java

  • Single inheritance: one subclass extends one superclass
  • Multilevel inheritance: a class extends a class that already extends another
  • Hierarchical inheritance: multiple subclasses extend the same superclass
  • Multiple inheritance of classes is not supported directly; Java uses interfaces for multiple type contracts

Important Rules

  • Constructors are not inherited
  • Private members are not directly accessible in subclasses
  • Overriding requires a compatible method signature and return type
  • super can access parent behaviour or constructors
  • final classes cannot be extended and final methods cannot be overridden

Inheritance vs Composition

Use inheritance when the subtype truly represents the parent abstraction. Prefer composition when one object simply uses another object's capability, because composition often creates looser coupling.

Continue Learning at Forsk Coding School