Inheritance — Extending Classes
Inheritance lets a class (subclass) acquire fields and methods from another class (superclass) using the extends keyword. It models IS-A relationships — an EBook IS-A Book. Inheritance enables code reuse and sets the foundation for polymorphism. However, it also introduces tight coupling — use it judiciously.
extends — IS-A Relationship
// Superclass — the general concept
public class LibraryItem {
private final String id;
private String title;
private boolean available;
public LibraryItem(String id, String title) {
this.id = id;
this.title = title;
this.available = true;
}
public boolean borrow() {
if (!available) return false;
available = false;
return true;
}
public void returnItem() { available = true; }
// Getters
public String getId() { return id; }
public String getTitle() { return title; }
public boolean isAvailable() { return available; }
@Override
public String toString() {
return String.format("[%s] %s (%s)", id, title, available ? "Available" : "Borrowed");
}
}
// Subclass — inherits all LibraryItem behaviour, adds its own
public class Book extends LibraryItem {
private final String isbn;
private final String author;
private double price;
public Book(String isbn, String title, String author, double price) {
super(isbn, title); // CALLS LibraryItem constructor — must be FIRST
this.isbn = isbn;
this.author = author;
this.price = price;
}
// Book-specific method
public String getCitation() {
return String.format("%s. "%s." (ISBN: %s)", author, getTitle(), isbn);
}
@Override
public String toString() {
// super.toString() reuses parent's implementation
return super.toString() + String.format(" — %s, $%.2f", author, price);
}
}
// Subclass — digital book, different attributes
public class EBook extends LibraryItem {
private final String isbn;
private final String downloadUrl;
private int maxSimultaneousReaders;
private int currentReaders;
public EBook(String isbn, String title, String downloadUrl, int maxReaders) {
super(isbn, title);
this.isbn = isbn;
this.downloadUrl = downloadUrl;
this.maxSimultaneousReaders = maxReaders;
this.currentReaders = 0;
}
// Override borrow — EBook can be borrowed by multiple readers simultaneously
@Override
public boolean borrow() {
if (currentReaders >= maxSimultaneousReaders) return false;
currentReaders++;
return true; // Note: isAvailable() still true if under limit
}
@Override
public void returnItem() {
if (currentReaders > 0) currentReaders--;
}
}Inheritance Rules
- Java supports single inheritance only — a class can extend ONE superclass (unlike C++).
- All Java classes implicitly extend Object if no explicit superclass defined.
- super() calls the parent constructor — must be first statement in subclass constructor.
- super.method() calls the parent's version of an overridden method.
- final class — cannot be extended: String, Integer, Math are all final.
- final method — cannot be overridden in a subclass.
Quick Quiz
Tip
Tip
Practice Inheritance Extending Classes in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Java is built on these four fundamental OOP principles
Practice Task
Note
Practice Task — (1) Write a working example of Inheritance Extending Classes from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Common Mistake
Warning
A common mistake with Inheritance Extending Classes is skipping edge case testing — empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready java code.
Key Takeaways
- Inheritance lets a class (subclass) acquire fields and methods from another class (superclass) using the extends keyword.
- Java supports single inheritance only — a class can extend ONE superclass (unlike C++).
- All Java classes implicitly extend Object if no explicit superclass defined.
- super() calls the parent constructor — must be first statement in subclass constructor.