Fundamentals 5 min read

Why Use final Classes and Methods in Java: A Practical Guide

The article explains how the Java final keyword can be applied to classes and methods to prevent inheritance and overriding, provides syntax and concrete code examples—including a final class and a final method in a chess algorithm—and advises using final for safety in constructors.

Lisa Notes
Lisa Notes
Lisa Notes
Why Use final Classes and Methods in Java: A Practical Guide

The final keyword in Java is commonly used to mark variables, methods, or classes as immutable, meaning they cannot be changed after definition. When applied to classes or methods, it prevents subclassing and method overriding, which can improve safety and predictability in object‑oriented designs.

Using final to Declare a Class

If a class is declared with final, it cannot have any derived subclasses; the class is effectively sealed. This is often done for important classes where inheritance could introduce unwanted behavior. The syntax is:

final class ClassName {
    // class body
}

Example:

public final class FinalClass {
    private String str = "这是一个fnal类.";
    private String strl = "它因为不能被继承,所以不可能有派生类.";
    public static void main(String[] args) {
        // Create an instance of the class
        FinalClass finalClass = new FinalClass();
        // Output the values of the member variables
        System.out.println(finalClass.str);
        System.out.println(finalClass.strl);
    }
}

The example defines a class named FinalClass. Because the class is final, it cannot be extended, and all methods inside it are implicitly non‑overridable.

Using final to Declare a Method

When a method is marked final, subclasses cannot override it. The declaration looks like: protected final void doXXX() { ... } Preventing overriding can avoid problems where a subclass changes essential behavior. For instance, in a chess program the rule that White moves first is fixed; the method getFirstPlayer() should be final to ensure the rule cannot be altered:

class ChessAlgorithm {
    enum ChessPlayer { WHITE, BLACK }; // enum type
    // ... other members ...
    // Declare a method that cannot be overridden
    final ChessPlayer getFirstPlayer() {
        return ChessPlayer,WHITE;
    }
    // ... other members ...
}

The author notes that, in general, methods called from a constructor should be declared final to prevent subclasses from modifying their behavior during object construction.

Overall, using final for classes and methods helps enforce immutability, reduces the risk of unintended overrides, and contributes to more robust Java applications.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Javaobject-orientedimmutabilityclassmethodfinal
Lisa Notes
Written by

Lisa Notes

Lisa's notes: musings on daily life, work, study, personal growth, and casual reflections.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.