Inheritance and super
What you will learn
Inheritance lets a class specialize another class. A subclass has a superclass, and may add operations or replace inherited behavior. Use inheritance when the specialized object can honor the superclass contract, not simply because two classes contain similar code.
After this lesson you can:
- Construct a subclass by calling its superclass constructor.
- Distinguish inherited operations from inaccessible private state.
Use a separate Main.java for each example. Compile with javac Main.java and run with java Main. The code is compatible with Java 21; Java 25 execution is not yet verified.
class Parcel { private final int weight; Parcel(int weight) { if (weight <= 0 || weight > 100) throw new IllegalArgumentException("weight"); this.weight = weight; } public int weight() { return weight; } public int fee() { return weight * 20; }}class ExpressParcel extends Parcel { ExpressParcel(int weight) { super(weight); } public int fee() { return super.fee() + 100; }}public class Main { public static void main(String[] args) { ExpressParcel parcel = new ExpressParcel(3); System.out.println(parcel.weight()); System.out.println(parcel.fee()); }}Expected output:
3160The inherited accessor reads the validated weight. The express fee extends the ordinary calculation by 100 cents.
Construct the superclass part before using the object
The declaration class ExpressParcel extends Parcel establishes a subtype relationship. An ExpressParcel can be assigned to a Parcel variable, and inherits accessible operations such as weight(). The Parcel constructor still validates the weight; its private field remains private. The subclass can call the inherited accessor instead of duplicating the field.
Constructors are not inherited. A subclass constructor invokes a constructor of its direct superclass, either directly with super(...) or through another constructor of its own class using this(...). With neither explicit invocation, Java supplies super(), which needs an accessible no-argument superclass constructor. In Java 21, the explicit invocation must be first. Java 25 permits a restricted prologue before that invocation; the examples use the older portable form and do not test the newer syntax. The expression super.fee() invokes the superclass implementation of fee, rather than calling the overriding method again. It does not construct another object.
For access control, private superclass members are not inherited. Protected members permit same-package access plus qualified subclass access subject to additional rules across packages. These examples use the unnamed package; do not infer cross-package access from them. Prefer a protected operation with a clear contract over exposing mutable protected fields. A final class cannot be extended; a final instance method cannot be overridden.
class Ticket { private final String zone; Ticket(String zone) { this.zone = zone; System.out.println("base"); } public String zone() { return zone; }}class DayTicket extends Ticket { DayTicket() { this("A"); } DayTicket(String zone) { super(zone); System.out.println("day"); }}public class Main { public static void main(String[] args) { DayTicket ticket = new DayTicket(); System.out.println(ticket.zone()); }}Expected output:
basedayAThe no-argument constructor delegates to the other DayTicket constructor, which invokes Ticket before printing day.
Check a boundary or variation
class Account { private final int balance; Account(int balance) { this.balance = balance; } protected int balance() { return balance; }}class Savings extends Account { Savings(int balance) { super(balance); } public int bonus() { return balance() / 10; }}public class Main { public static void main(String[] args) { Savings account = new Savings(90); System.out.println(account.bonus()); }}Expected output:
9The subclass uses the superclass accessor, not its private field.
Common mistakes
Do not write another weight field in ExpressParcel or assume a superclass constructor is inherited. Duplicating state can create disagreement; call super with the required arguments and reuse the superclass contract.
Practice
Create Admission with a private guests count from 1 to 50 and a price() of 300 cents per guest. GuidedAdmission extends it, adds 500 cents per booking using super.price(), and preserves the same input restrictions. Check one guest, 50 guests, and invalid zero.
Pass guests to the superclass constructor. Use super.price() rather than duplicating the base calculation.
Next: Abstract classes and interfaces.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Inheritance and super — knowledge check
Apply the stated Java 21-compatible contracts and predict observable behavior.
Which declaration creates a subclass?
What is super(weight) used for?
If no constructor invocation is written, what must be available?
Can ExpressParcel directly read Parcel’s private weight field?
What does super.fee() do in ExpressParcel?