OBJECT-ORIENTED PROGRAMMING

Java inheritance

Inheritance lets a new class acquire another class's fields and methods. Shared code stays in a base class, while specialized classes extend or adapt it.

Superclass and subclass

Key Java inheritance concepts
ConceptMeaningPurpose
SuperclassBase or parent classThe class whose members are inherited
SubclassDerived or child classA class that inherits and extends a base class

A Java class extends another class using the keyword extends.

Inheritance with extends

A simple everyday example: a book is a kind of publication, so the class Knyga can extend the class Leidinys.

class Leidinys {
    protected String pavadinimas = "Mažasis princas";

    public void rodytiPavadinima() {
        System.out.println("Leidinys: " + pavadinimas);
    }
}

class Knyga extends Leidinys {
    private int puslapiuSkaicius = 96;

    public void rodytiPuslapius() {
        System.out.println("Puslapių: " + puslapiuSkaicius);
    }

    public static void main(String[] args) {
        Knyga knyga = new Knyga();
        knyga.rodytiPavadinima();
        knyga.rodytiPuslapius();
    }
}
Console output

Leidinys: Mažasis princas
Puslapių: 96

  1. class Leidinys defines what all publications have: a title and a method to display it.
  2. class Knyga extends Leidinys says that a book acquires the accessible Leidinys members.
  3. A book also has its own field, puslapiuSkaicius and the method rodytiPuslapius().
  4. The created Knyga object can call both its inherited method and its own method.

Using inherited members

class Leidinys {
    protected String kategorija = "Vaikų literatūra";

    public void rodytiKategorija() {
        System.out.println("Kategorija: " + kategorija);
    }
}

class Knyga extends Leidinys {
    public void rodytiInformacija() {
        rodytiKategorija();
        System.out.println("Kategorijos pavadinime yra "
                + kategorija.length() + " simbolių.");
    }

    public static void main(String[] args) {
        Knyga knyga = new Knyga();
        knyga.rodytiInformacija();
    }
}
Console output

Kategorija: Vaikų literatūra
Kategorijos pavadinime yra 16 simbolių.

  1. Knyga inherits the protected field kategorija and the public method rodytiKategorija().
  2. The method rodytiInformacija() first calls the inherited method.
  3. It then directly uses the inherited protected field and counts the text's characters.
private members are not directly accessible to a subclass.

The base class can protect them and provide controlled access through protected or public methods.

The base class constructor

super(...) calls the base class constructor and must be the first statement in the subclass constructor:

class Leidinys {
    protected String pavadinimas;

    Leidinys(String pavadinimas) {
        this.pavadinimas = pavadinimas;
    }
}

class Knyga extends Leidinys {
    private int puslapiuSkaicius;

    Knyga(String pavadinimas, int puslapiuSkaicius) {
        super(pavadinimas);
        this.puslapiuSkaicius = puslapiuSkaicius;
    }

    public void rodytiInformacija() {
        System.out.println(pavadinimas + ": "
                + puslapiuSkaicius + " puslapiai");
    }

    public static void main(String[] args) {
        Knyga knyga = new Knyga("Mažasis princas", 96);
        knyga.rodytiInformacija();
    }
}
Console output

Mažasis princas: 96 puslapiai

  1. When creating a Knyga object, the constructor receives a title and page count.
  2. super(pavadinimas) passes the title to the base class's Leidinys constructor.
  3. The base class stores the shared title, while Knyga stores the page count specific to it.

Overriding an inherited method

A subclass can provide its own implementation of an inherited method:

class Leidinys {
    public void rodytiTipa() {
        System.out.println("Tai yra leidinys");
    }
}

class Knyga extends Leidinys {
    @Override
    public void rodytiTipa() {
        System.out.println("Tai yra knyga");
    }

    public static void main(String[] args) {
        Leidinys leidinys = new Leidinys();
        Knyga knyga = new Knyga();

        leidinys.rodytiTipa();
        knyga.rodytiTipa();
    }
}
Console output

Tai yra leidinys
Tai yra knyga

  1. Leidinys provides the general rodytiTipa() method behavior.
  2. Knyga inherits this method but overrides it with its own version.
  3. @Override helps the compiler verify that this method exists in the base class.
  4. The same method name can therefore produce different results for different objects.

When should you use inheritance?

  • When the subclass is genuinely a more specific kind of the base class.
  • When several classes share state and behavior.
  • When shared code can be maintained in one base class.
  • When inherited behavior should be specialized by overriding methods.

Do not use inheritance solely to access another class's code. If the objects have no natural “is-a” relationship, composition is often more suitable: storing one object in another object's field.

Restricting inheritance with final

final class cannot have subclasses:

final class BibliotekosKortele {
}

// Error: a final class cannot be extended
class SpecialiKortele extends BibliotekosKortele {
}
Compiler error

error: cannot inherit from final BibliotekosKortele

BibliotekosKortele is marked final, so Java does not allow creating a class that extends it.

final can also prevent a particular method from being overridden:

class Leidinys {
    public final void rodytiIsbn() {
        System.out.println("ISBN: 978-609-00-0000-0");
    }
}

class Knyga extends Leidinys {
    @Override
    // Error: a final method cannot be overridden
    public void rodytiIsbn() {
    }
}
Compiler error

error: rodytiIsbn() in Knyga cannot override rodytiIsbn() in Leidinys
overridden method is final

A subclass inherits the final method and may use it, but cannot change its behavior.

Important Java inheritance rules

  • A class can directly extend only one other class.
  • All classes directly or indirectly extend Object.
  • Constructors are not inherited, but a subclass constructor calls a base class constructor.
  • private members are not directly accessible in a subclass.
  • A subclass can add its own fields and methods and override permitted methods.