The final keyword is used for many purposes. The different use of final keyword is shown with sample program below.
Final Variable
A final variable can not be updated. Generally, final variables are used where the values remain unchnaged throughout the application. For example, the value of Pi that does not change.
Final Method
Final methods can not be overwritten. Hence, if you define a method that can not be overwritten in sub-class, the method should be declared as final method.
Final Class
If a class is declared as final, the class can not be inherited.
The following class will not compile, as the class B
inherits class A
, which is final.
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.