ByteBrawl - Final - Non Access Modifiers - Encapsulation

Non Access Modifiers - Final

The final keyword's effect varies wildly depending on what it is applied to. In some ways, it is the opposite of abstract because it prevents further modification or extension rather than requiring it.

"subclass"

final with Variables

When applied to variables, the final modifier makes them constants, meaning their value can only be assigned once, either at the point of declaration or within the constructor. This is useful for values that should remain constant throughout the program, such as mathematical constants or configuration values. Any attempt to reassign a final variable will result in a compilation error.

final with Methods

When applied to methods, the final modifier prevents the method from being overridden by subclasses. If you try to override a final method in a subclass, you'll get a compilation error.

final with Classes

When applied to classes, the final modifier prevents the class from being subclassed. This is useful for classes that are not meant to be extended. If you try to extend a final class, you'll get a compilation error.