# Inheritance, super and Constructor Chaining — Object-Oriented Programming (OOP)

Source: https://www.skillbyai.com/en/oop/i-basics

> Create subclasses that reuse and extend a superclass correctly.

## Is-a relationships

**Inheritance** lets a **subclass** (child) acquire the fields and methods of a **superclass** (parent) and add or change behaviour: `class SavingsAccount extends Account`. It models an **is-a** relationship: a savings account *is an* account. In Java every class ultimately inherits from `Object`; a class can extend **one** superclass. A subclass constructor must initialise its parent first: Java inserts a call to the parent's no-argument constructor, or you call a specific one with **`super(...)`** as the **first statement**. **`super.method()`** calls the parent's version of an overridden method. `private` members are not accessible in the subclass (they exist but are hidden); `protected` members are. Inheritance types often listed in syllabi: **single**, **multilevel** (A → B → C), **hierarchical** (several classes extend one parent), and **multiple** inheritance (one class with several parents), which Java does not allow for classes but C++ does; Java achieves similar flexibility with multiple interfaces.

## A class hierarchy

Subclasses inherit from a parent and add their own specialised behaviour.

![A single top box with three lines down to three child boxes, one of which has a further child below it.](assets/figures/oop/section-3-map.svg) — Figure 3.1 — Single, hierarchical and multilevel inheritance.

## A subclass with constructor chaining and super calls

SavingsAccount reuses Account and adds interest.

```java
public class Account {
    protected final String owner;
    protected double balance;

    public Account(String owner, double opening) {
        this.owner = owner;
        this.balance = opening;
    }

    public String summary() { return owner + ": " + balance; }
}

public class SavingsAccount extends Account {
    private final double ratePercent;

    public SavingsAccount(String owner, double opening, double ratePercent) {
        super(owner, opening);                    // must come first
        this.ratePercent = ratePercent;
    }

    public void addMonthlyInterest() {
        balance += balance * ratePercent / 100 / 12;   // protected field from Account
    }

    @Override
    public String summary() {
        return super.summary() + " (savings @ " + ratePercent + "%)";
    }
}
```

## Is-a must be true in every situation

Before extending a class, check that the subclass can be used wherever the parent is expected, without surprises. "A car has an engine" is composition, not inheritance.

**Quiz:** In a Java subclass constructor, where must an explicit call to super(...) appear?

- [x] As the first statement
- [ ] Anywhere in the constructor
- [ ] As the last statement
- [ ] Only in static methods

*Answer:* As the first statement. The parent must be initialised before the subclass constructor body runs, so super(...) comes first.
