Java Tutorial: Inheritance
Extend classes with extends, use super, override methods, understand the Object class, and build class hierarchies.
Build on what exists
Inheritance lets you create specialised versions of existing classes — inheriting all their code and adding or changing what's different.
`AdminUser` extends `User`: it gets everything User has for free, then adds admin-specific capabilities. This eliminates massive code duplication in real applications.
What you'll learn in this Java inheritance tutorial
This interactive Java tutorial has 6 hands-on exercises. Estimated time: 20 minutes.
- extends — inherit everything — `class Child extends Parent` gives Child all of Parent's fields and methods for free.
- super() — parent constructor — When a child class defines its own constructor, it **must** call `super(...)` as the first statement.
- @Override — replace the parent method — `@Override` replaces a parent method with a new implementation.
- instanceof and casting — `instanceof` checks if an object is of a given type — including parent types.
- final and abstract classes — - `final class` — cannot be extended (String is final!)
- Build a vehicle hierarchy — Final challenge — no starter code.
Java Inheritance concepts covered
- Build on what exists