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
| Concept | Meaning | Purpose |
|---|---|---|
| Superclass | Base or parent class | The class whose members are inherited |
| Subclass | Derived or child class | A 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();
}
}Leidinys: Mažasis princas
Puslapių: 96
class Leidinysdefines what all publications have: a title and a method to display it.class Knyga extends Leidinyssays that a book acquires the accessibleLeidinysmembers.- A book also has its own field,
puslapiuSkaiciusand the methodrodytiPuslapius(). - The created
Knygaobject 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();
}
}Kategorija: Vaikų literatūra
Kategorijos pavadinime yra 16 simbolių.
Knygainherits theprotectedfieldkategorijaand the public methodrodytiKategorija().- The method
rodytiInformacija()first calls the inherited method. - It then directly uses the inherited
protectedfield 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();
}
}Mažasis princas: 96 puslapiai
- When creating a
Knygaobject, the constructor receives a title and page count. super(pavadinimas)passes the title to the base class'sLeidinysconstructor.- The base class stores the shared title, while
Knygastores 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();
}
}Tai yra leidinys
Tai yra knyga
Leidinysprovides the generalrodytiTipa()method behavior.Knygainherits this method but overrides it with its own version.@Overridehelps the compiler verify that this method exists in the base class.- 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 {
}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() {
}
}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.
privatemembers are not directly accessible in a subclass.- A subclass can add its own fields and methods and override permitted methods.