The final keyword can be used with class, method, variables & parameters.
Final Class
When a final keyword is used in class declaration means a class can not be subclassed. So all methods are indirectly marked as final and can not be overridden.
Note - We can not mark class both abstract and final because abstract class must be subclassed and final class can not be subclassed.
Example
The above declaration will give this error [The type TestFinal cannot subclass the final class Final]
Final Method
When a final keyword is used with the method means that method can not be overridden in the subclass.
Note- We can not declare the method both abstract and final.
Example
The above declaration will give this error in the Dog class Cannot override the final method from Animal
Final Variable
When a variable is declared final means its value can not be changed after its initialization. But it's not necessary to initialize variables at the time of declaration.
Example
Final Parameters
When we used final parameters in the method means we can not change its value inside the method.
Example
We have completed this article. Let me know if you have any questions.
Visit below the useful post
Thanks
Happy Learning!
0 Comments
If you have any doubts let me know.