Java
Thread
Abstract Class
Final Method
Object-Oriented Programming

Why is Thread not an abstract class and start not final?

Object-Oriented Design practice on Codemia

Turn requirements into classes, and defend the design, on the problems that come up in OOD rounds.

Practice OOD

Threads in Java are a crucial part of concurrent programming, enabling developers to execute multiple tasks in parallel. This article explores two design decisions in Java's threading model: why the `Thread` class is not abstract and why the `start()` method is not marked as `final`. Understanding these design choices can provide insights into Java's approach to concurrency and object-oriented principles.

Thread Class: Why It's Not Abstract

Background on Thread Class

In Java, the `Thread` class is part of the `java.lang` package and represents a thread of execution. Threads are a fundamental part of concurrent programming, allowing for applications to run code asynchronously and improve throughput or responsiveness.

Design Explanation

  1. Flexible Instantiation:
    • The `Thread` class is designed to be directly instantiated or extended, providing flexibility. If `Thread` were abstract, developers would be forced to subclass it even when just an instance of a thread was needed for execution. By keeping it concrete, Java allows developers the choice to use `Thread` directly or through extension.
  2. Ease of Use:
    • Making `Thread` a concrete class simplifies the creation of new threads. Developers can simply instantiate it and pass a `Runnable` to its constructor without needing their own subclass. This is particularly useful for straightforward concurrency tasks.
  3. Historical Context:
    • Early designs of the Java concurrency model aimed at simplicity. As concurrency was a relatively advanced topic at Java's inception, reducing the entry barriers for developers was a priority.

Example

  • The `start()` method is not marked `final` to allow developers greater flexibility when subclassing `Thread`. The design follows the open-closed principle, allowing methods to be overridden where necessary for customization.
  • Some applications may require custom behaviors before or after a thread begins execution. By not marking `start()` as `final`, developers can override it to incorporate logging, security checks, or initialization sequences before delegating to `super.start()`.
  • The non-final nature of `start()` contributes to the backward compatibility of Java applications. Early design decisions prioritized making Java extensible without breaking existing user code when new features are introduced.
  • Concurrency in Modern Java:
  • Future-Proofing Code:

Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Object-Oriented Design practice on Codemia

Turn requirements into classes, and defend the design, on the problems that come up in OOD rounds.

Practice OOD

All Rights Reserved.