Skip to main content

Lesson 3 of 9

Abstract Classes - Partial Implementation

An abstract class is a class that cannot be instantiated directly - it exists to be extended. It can have both concrete (implemented) and abstract (unimplemented) methods. Use an abstract class when you have common behaviour to share with subclasses but at least one method that every subclass MUST implement in its own way.

12 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

Abstract Class vs Interface vs Concrete Class

  • Abstract class: can have fields, constructors, concrete methods, and abstract methods. Cannot be instantiated.
  • Interface: pure contract (abstract methods + default methods). No instance fields (only constants). No constructors.
  • Concrete class: fully implemented, can be instantiated.
  • IF you share code (fields, constructor logic, helper methods) → prefer abstract class.
  • IF you define a contract multiple unrelated classes can implement → prefer interface.
  • A class can extend ONE abstract class but implement MULTIPLE interfaces.

Abstract Class in the Library System

Abstract Class in the Library System
// Abstract superclass - common behaviour for all borrowable items
public abstract class LibraryItem {
    private final String id;
    private final String title;
    private boolean available = true;

    // Constructor - abstract classes CAN have constructors (called via super())
    public LibraryItem(String id, String title) {
        this.id    = id;
        this.title = title;
    }

    // Concrete method - shared by all subclasses
    public boolean borrow() {
        if (!isAvailable()) return false;
        available = false;
        return true;
    }

    public void returnItem() { available = true; }
    public String getId()        { return id; }
    public String getTitle()     { return title; }
    public boolean isAvailable() { return available; }

    // Abstract methods - every subclass MUST implement these
    public abstract String getDisplaySummary();
    public abstract double calculateLateFee(int daysLate);
    public abstract String getCategory();

    // Template method pattern - calls abstract hook
    public void printLabel() {
        System.out.println("=== " + getCategory() + " LABEL ===");
        System.out.println(getDisplaySummary());  // calls subclass's version
        System.out.println("Available: " + available);
    }
}

// Concrete subclass - MUST implement all abstract methods
public class Book extends LibraryItem {
    private final String author;
    private final String isbn;
    private double price;

    public Book(String isbn, String title, String author, double price) {
        super(isbn, title);
        this.isbn   = isbn;
        this.author = author;
        this.price  = price;
    }

    @Override public String getDisplaySummary() {
        return String.format(""%s" by %s (ISBN: %s) - $%.2f", getTitle(), author, isbn, price);
    }
    @Override public double calculateLateFee(int daysLate) { return daysLate * 1.00; }
    @Override public String getCategory() { return "BOOK"; }
}

public class AudioBook extends LibraryItem {
    private final String narrator;
    private final int runtimeMinutes;

    public AudioBook(String id, String title, String narrator, int runtimeMinutes) {
        super(id, title);
        this.narrator = narrator;
        this.runtimeMinutes = runtimeMinutes;
    }

    @Override public String getDisplaySummary() {
        return String.format(""%s" narrated by %s (%d min)", getTitle(), narrator, runtimeMinutes);
    }
    @Override public double calculateLateFee(int daysLate) { return daysLate * 0.75; }
    @Override public String getCategory() { return "AUDIOBOOK"; }
}

// LibraryItem item = new LibraryItem(...);  // ERROR - cannot instantiate abstract class

Quick Quiz

Tip

Tip

Practice Abstract Classes Partial Implementation in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagram…

Visual architecture and concepts for Abstract Classes - Partial Implementation

Practice Task

Note

Practice Task - (1) Write a working example of Abstract Classes Partial Implementation 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 Abstract Classes Partial Implementation 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

  • An abstract class is a class that cannot be instantiated directly - it exists to be extended.
  • Abstract class: can have fields, constructors, concrete methods, and abstract methods. Cannot be instantiated.
  • Interface: pure contract (abstract methods + default methods). No instance fields (only constants). No constructors.
  • Concrete class: fully implemented, can be instantiated.

Topics in This Module

0/9 done