What is the point of final class in Java?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
In Java, the keyword final can be applied to variables, methods, and importantly, classes. When final is used in the context of a class, it serves a specific purpose by preventing the class from being inherited by other classes. This feature has several implications for the design and functionality of Java applications, affecting aspects such as security, design clarity, immutability, and optimization. Understanding why and when to use final classes can be crucial for developing robust and efficient Java applications.
The Purpose of Final Classes
1. Immutability
One of the primary reasons for making a class final is to create immutable classes. An immutable class is one whose state cannot be changed once it has been constructed. String, Integer, and other wrapper classes in Java are examples of immutable classes that are declared as final. Immutability is desired when the object needs to remain constant throughout its lifecycle to prevent accidental or malicious changes, preserve integrity, or provide simple thread safety.
2. Preventing Inheritance
Final classes are used to put a restriction on the inheritance feature of object-oriented programming. When a class is declared as final, it cannot be extended or subclassed. This is particularly useful when the class is not designed to be inherited or when extending it could lead to software instability or security issues. For instance, if a class exposes sensitive data or critical functionality, allowing it to be subclassed might lead to leaks or breaks in the intended use of the class.
3. Encapsulation and Security
Final classes help in better encapsulation. Encapsulation is the bundling of data (variables) and methods that operate on the data into a single unit or class. By making a class final, you are ensuring that no subclass can alter its behavior and compromise encapsulation. This aspect is closely related to security, as it prevents unauthorized parties from altering the fundamental behavior of the class through inheritance.
4. Optimization
From a performance perspective, final classes allow for certain optimizations by the Java compiler. Since the compiler knows that the class cannot be extended, method invocations can be resolved at compile-time rather than runtime. This is known as static binding. With static binding, the method to be executed is known at compile time, leading to faster method invocation compared to dynamic binding (used in polymorphic method invocations).
Technical Example
Consider a simple final class:
In this example, you cannot extend the Calculator class:
Use Cases of Final Classes
- Constant Utility Classes: Utility classes which store a bunch of static methods and variables like
MathorCollections. - Secure Class Behavior: When the developer intends to secure the class behavior against any modification through inheritance, potentially for security reasons.
- Creating Immutable Classes: As part of creating immutable data classes which should not be changed once instantiated.
Summary Table
| Feature | Benefit |
| Immutability | Ensures object’s state cannot be altered. |
| Prevent Inheritance | Protects against unintended subclass modifications. |
| Encapsulation | Keeps the behavior integral by preventing external modification through subclassing. |
| Optimization | Facilitates compiler optimizations like static binding. |
In conclusion, understanding the purpose and usage of final classes in Java enhances the capability to design robust, efficient, and secure applications. Whether creating immutable objects or securing class behavior, final classes prove to be a powerful tool in the Java programming language, helping maintain code integrity and performance.
Related reading
- What is the point of the diamond operator (<>) in Java?
- What is the proper annotation since SpringApplicationConfiguration, WebIntegration, is deprecated in Spring Boot Framework?
- What is the proper way to re-attach detached objects in Hibernate?
- What is the purpose of CompletableFuture's complete method?
- What is the purpose of mvnw and mvnw.cmd files?
- What is the purpose of mvnw and mvnw.cmd files?
- What is the purpose of passing parameter to synchronized block?
- What is the purpose of the default keyword in Java?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.