As we know that inheritance enables us to reuse existing code, sometimes we do need to set limitations on extensibility for various reasons; the final keyword allows us to do exactly that.
In this short article, we’ll take a look at what the final keyword means for classes, methods, and variables -
- The final keyword may be applied to a variable, indicating the value of the final variable can not be changed (It will be constant).
- The final keyword may be applied to a class, indicating the class may not be extended (subclassed).
- The final keyword may be applied to a method, indicating the method may not be overridden in any subclass
Let's discuss usage of a final keyword with respect to variable, method, and class.
Java final variable
If you make any variable as final, you cannot change the value of a final variable(It will be constant).
Example of final variable
The below diagram demonstrates that the final variable name can't be reassigned which will result in compile time error.
Java final method
The final keyword may be applied to a method, indicating the method may not be overridden in any subclass.
Sometimes we don’t need to prohibit a class extension entirely, but only prevent overriding of some methods. A good example of this is the Thread class. It’s legal to extend it and thus create a custom thread class. But its isAlive() methods is final.
Java final method example
The below diagram demonstrates that the final method can't be overridden which will result in compile time error:
Java final class
The final keyword may be applied to a class, indicating the class may not be extended (subclassed).
If we look at the code of Java core libraries, we’ll find many final classes there. For example String, Wrapper classes and BigDecimal, BigInteger etc.Java final class example
The below diagram demonstrates that the final class can't be extended which will result in compile time error.
What is blank or uninitialized final variable?
A final variable that is not initialized at the time of declaration is known an as a blank final variable.
If you want to create a variable that is initialized at the time of creating an object and once initialized may not be changed, it is useful. For example PAN CARD number of an employee.
It can be initialized only in the constructor.
Example of blank final variable
class Student{
int id;
String name;
final String PAN_CARD_NUMBER;
...
}
Note that the final PAN_CARD_NUMBER variable not initialized. We can initialize a blank final variable only in a constructor.
Summary
- Variables marked as final can’t be reassigned.
- Methods marked as final cannot be overridden.
- Classes marked as final can’t be extended.
All Java Keywords
- abstract Java Keyword
- assert Java Keyword
- boolean Java Keyword
- break Java Keyword
- byte Java Keyword
- case Java Keyword
- catch Java Keyword
- char Java Keyword
- class Java Keyword
- continue Java Keyword
- default Java Keyword
- do Java Keyword
- double Java Keyword
- else Java Keyword
- enum Java Keyword
- extends Java Keyword
- final Java Keyword
- finally Java Keyword
- float Java Keyword
- for Java Keyword
- if Java Keyword
- implements Java Keyword
- import Java Keyword
- instanceof Java Keyword
- int Java Keyword
- interface Java Keyword
- long Java Keyword
- native Java Keyword
- new Java Keyword
- package Java Keyword
- private Java Keyword
- protected Java Keyword
- public Java Keyword
- return Java Keyword
- short Java Keyword
- static Java Keyword
- strictfp Java Keyword
- super Java Keyword
- switch Java Keyword
- synchronized Java Keyword
- this Java Keyword
- throw Java Keyword
- throws Java Keyword
- transient Java Keyword
- try Java Keyword
- void Java Keyword
- volatile Java Keyword
- while Java Keyword
Comments
Post a Comment
Leave Comment