What is Inheritance?
Inheritance is an OOP mechanism that derives a new class from an existing one, promoting code reuse and establishing hierarchical relationships.
- Create and interact with objects that inherit from other objects
- Create subclasses using the
extendskeyword - Define reusable classes based on inheritance hierarchies
- Use the
protectedmodifier for controlled access - Understand constructors in inheritance chains
- Override methods in subclasses
Explore the Topics
Before creating an inheritance hierarchy, verify these points:
Inheritance Basics
Inheritance allows defining a class in terms of a previously defined class using the extends keyword.
Without inheritance, related classes must duplicate shared fields and methods:
Inheritance is appropriate when an "is a" relationship exists between two classes. Drag each subclass to its correct superclass:
Create a GraduateStudent class that extends Student and adds a researchTopic field.
Notice: super(name, gpa) must be the first line. The @Override annotation ensures toString() properly overrides the parent.
Access Rules
Complete reference for Java access modifiers, visibility spectrum, and interactive examples.
| Modifier | Same Class | Same Package | Subclass | Different Package | Scope / Rule |
|---|---|---|---|---|---|
| public | ✓ | ✓ | ✓ | ✓ | Unrestricted access from anywhere |
| protected | ✓ | ✓ | ✓ | ✗ | Accessible to the class, its subclasses, and classes in the same package |
| default | ✓ | ✓ | ✗ | ✗ | Restricted to within the same package only |
| private | ✓ | ✗ | ✗ | ✗ | Restricted to within the defining class only |
Java provides four levels of access control, from most restrictive to most accessible:
Visibility increases →
Click on each member to see if it's accessible from the selected context:
public in the superclass, it must remain public in the subclass. However, you can increase visibility (e.g., protected → public).
Only public members are visible from an unrelated class. Both protected and private are hidden.
public members of both Super and Sub objects.Everything is visible except the private members of its superclass.
public and protected members inherited from the parent, plus its own members.Given class Vehicle { public String make; protected int year; private String vin; } and class Truck extends Vehicle in a different package, which can Truck access?
Constructors in Inheritance
Constructors are not inherited. The parent constructor is always called before the child constructor body executes.
- Constructors of a superclass are NOT inherited by subclasses
- If the subclass constructor does not explicitly call
super(), Java implicitly calls the no-arg constructor of the parent - If the parent class has no no-arg constructor, the subclass will not compile
- The parent constructor call must be the first line of the subclass constructor
super(s) to explicitly call the existing Employee(String) constructor.
Step through the constructor chain when new Faculty() is called:
new Faculty() — JVM enters Faculty's no-arg constructor.This code has a compile error. Can you spot and fix it?
Problem: Dog() implicitly calls super(), but Animal has no no-arg constructor!
Fix: Change to public Dog(String name) { super(name); ... }
The super Keyword
A reference variable used to refer to the immediate parent class object.
super() must be the first statement in the subclass constructor. You cannot place other code before it.
When the parent field is protected, the subclass can directly access it without super:
What is wrong with this code?
Given a Person class with toString() returning "Person: Alice", write Employee's toString() that appends company info:
super.toString() calls the parent's version, and you extend it with additional info. This is the most common override pattern!
Overriding vs. Overloading
Two different concepts that are often confused by students.
- Same method name
- Same parameter types
- Same return type
- Occurs in parent-child relationship
- Replaces the parent's implementation
a.p(10) → 10.0a.p(10.0) → 10.0
- Same method name
- Different parameter types
- Return type may differ
- Can be in same class or parent-child
- Adds a new version of the method
a.p(10) → 10 (int version)a.p(10.0) → 20.0 (parent's double version)
p(double) in both classes — in overloading, p(double) vs p(int).
For each pair, decide whether the subclass method overrides or overloads the parent method:
Knowledge Check
Test your understanding of Inheritance in Java. Click an answer to check it.