Which Class cannot be subclassed in Java?
Creating class hierarchies and extending existing classes through inheritance are basic concepts in Java programming. Not all classes, nevertheless, may be subclassed. Java has tools to limit inheritance for certain classes, one of which is the final keyword. In this section, we will examine the idea of final classes in Java and discover why they cannot be subclassed.
Understanding the final Keyword
Knowing the outcome, the final keyword in the Java programming language states that a class, method, or variable cannot be changed or overridden. When used on a class, the final keyword turns off any more inheritance and renders the class non-subclassable. A class designated as final cannot be further expanded or subclassed.
Purposes of Final Classes
There are numerous justifications for developers to decide to make a class final, including:
- Security: By declaring a class final, developers may ensure that key classes' implementation and behavior are unaltered. It stops unauthorized or unintentional changes that can jeopardize the system's security.
- Performance Improvement: Final classes offer chances for compiler improvement. Since the class cannot be expanded, the compiler may extrapolate from its behavior and create optimizations, making code execution more effective.
- Design Intent: Designers can build classes with a specific purpose, enabling them to be utilized exactly as is with no modifications. They may enforce this aim and stop any unintentional alterations or extensions that would compromise the original design by designating a class as final.
- API Stability: Preserving backward compatibility while creating libraries or frameworks is critical. Making classes final helps prevent any breaking changes resulting from subclass updates and guarantees that the public API stays stable.
- Classes Can Be Final and Abstract: It's important to remember that classes can be both final and abstract. This may seem incongruous since final classes forbid inheritance, yet abstract classes are designed to be extended. To stop future subclassing, an abstract class can be made final. The abstract final class can operate as a base class in certain circumstances, but any other class cannot extend it.
- Immutability: Final classes are frequently utilized when discussing immutable objects. Once created, immutable items cannot be changed. Developers ensure that an object's state cannot be changed by subclassing by designating the class as final.
- Inheritance and Final Classes: A class cannot be extended by another class once it has been declared final. A subclass can inherit the properties and methods of its superclass thanks to inheritance. A final class, on the other hand, cannot act as a superclass, which means that no other class may extend it. This limitation ensures that the final class doesn't change and can't be updated by subclassing.
- Final Methods and Variables: The final keyword may be used to designate specific methods and variables inside a class in addition to final classes. Any subclasses cannot override a method marked as final. Similarly, after a variable's value has been assigned, it cannot be modified if it is defined as final.
- Final Classes in an Inheritance Hierarchy: When a class in an inheritance hierarchy is marked final, the inheritance chain is broken. It is not possible to extend subclasses that were initially descended from the final class. This design choice was made on purpose since the intended implementation or behaviour of the final class should not be changed.
Examples of Final Classes
Certain classes in the Java Standard Library are declared final. Some notable examples include:
- lang.String: The String class represents an immutable sequence of characters. By making it final, Java ensures that no other class can inherit from String and modify its behavior.
- lang.Math: The Math class provides various mathematical functions and constants. It is declared final to maintain consistency and prevent any accidental modification of its functionality.
- lang.System: The System class provides access to system-related resources and operations. By making it final, Java ensures that critical system functionality remains intact and prevents unauthorized modifications.
In Java, the final keyword is essential for limiting inheritance and preventing classes from being subclassed. Final classes offer advantages such as security, speed optimization, upholding design intent, stable APIs, and immutability. Developers can protect important parts of their code and uphold the integrity of their ideas by designating a class as final. For Java programs to be reliable and safe, it is crucial to comprehend when and why to utilize final classes.
Remember that selecting a class final should be done carefully, considering the ramifications and technical specifications of your project.