100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace

What is a Final Class in OOP?

Learn what a final class is in OOP -- why it blocks subclassing, when to use it, and how String uses it -- with Java examples.

easyQ92 of 226 in Object Oriented Programming Est. time: 4 minsLast updated:
Open Code Lab

Expected Interview Answer

A final class is a class declared with the final keyword so it cannot be subclassed by any other class, locking its implementation as the end of the inheritance chain.

Marking a class final prevents any 'extends’ relationship from being formed against it, which the compiler enforces at compile time. This is used when a class’s behavior must never be altered by inheritance -- either for security (preventing malicious overrides), immutability guarantees (like Java’s String), or because the design is complete and stable. All methods in a final class are implicitly final too, since there is no subclass that could override them. Final classes can still implement interfaces and be composed into other objects; they simply cannot serve as a parent class.

  • Guarantees behavior cannot be altered through subclassing
  • Supports safe immutable value types
  • Prevents security-sensitive classes from being maliciously extended
  • Signals design intent: this class is complete

AI Mentor Explanation

The official leather match ball approved by the governing body is manufactured to an exact, sealed specification -- no team is allowed to produce a modified variant of it for play. That locked specification is a final class: nobody can extend or alter its structure. Every player uses the identical ball, which is the whole point, since consistency of the object itself is the guarantee being protected.

Step-by-Step Explanation

  1. Step 1

    Mark the class final

    Add the final keyword to the class declaration, e.g. public final class Config.

  2. Step 2

    Compiler blocks extension

    Any attempt to write class X extends Config fails to compile.

  3. Step 3

    Methods become implicitly final

    Since no subclass can exist, none of its methods can be overridden either.

  4. Step 4

    Composition still works

    Other classes can hold a reference to it or implement interfaces alongside it; only subclassing is blocked.

What Interviewer Expects

  • Correct definition: final on a class blocks subclassing entirely
  • A real-world reason to use it (immutability, security, API stability)
  • Awareness that java.lang.String is a canonical final class example
  • Understanding that final classes can still implement interfaces

Common Mistakes

  • Confusing a final class with a final variable or final method
  • Thinking a final class cannot have any subclass-like extension at all, including interfaces
  • Assuming final classes cannot be instantiated (they can be, freely)
  • Believing final is only about performance rather than design intent

Best Answer (HR Friendly)

A final class is one you mark so that no other class can inherit from it. It is a way of saying this class is complete and its behavior must never be changed through subclassing, which is exactly what you want for something like an immutable value type or a security-sensitive utility class.

Code Example

Final class blocking inheritance
public final class ApiKeyValidator {
    private final String secret;

    public ApiKeyValidator(String secret) {
        this.secret = secret;
    }

    public boolean isValid(String candidate) {
        return secret.equals(candidate);
    }
}

// The following fails to compile:
// class MaliciousValidator extends ApiKeyValidator { ... }

Follow-up Questions

  • What is the difference between a final class and a final method?
  • Why is java.lang.String declared final?
  • Can a final class implement an interface?
  • Does declaring a class final have any performance implications?

MCQ Practice

1. What happens if a class attempts to extend a final class?

Attempting to subclass a final class produces a compile-time error, not a runtime one.

2. Which of these is a well-known final class in Java?

java.lang.String is final, which is essential to guaranteeing its immutability.

3. Can a final class implement interfaces?

final only blocks being extended as a superclass; implementing interfaces is completely unaffected.

Flash Cards

Final class in one line?A class marked final so no other class can subclass it.

Why use it?To guarantee behavior cannot be altered by inheritance -- immutability, security, or design stability.

Canonical example?java.lang.String, whose immutability depends on being final.

Can it implement interfaces?Yes -- final only blocks being a superclass, not implementing interfaces.

1 / 4

Continue Learning