Final Keyword in Java

0
6

This Java Tutorial is all about Final Keyword in Java.
Final keyword in Java can be used with class, object, method and variables. It is used in order to restrict the user.

If final keyword is used along with the class, then that class cannot be extended.

If final keyword is prefixed before method then we will not be able to override that method.

If final keyword is used before variable/ object then we will not be able to reinitialize that object or variable.

  1. Syntax for using final keyword.
  2.  
  3. public final class A{
  4.  
  5.  
  6.  
  7. }
  8.  
  9. public final void method1 {
  10.  
  11. }
  12.  
  13. final int temp;

 

If a user is trying to violate any rule, user will get compile time error.

Facebook Comments
(Visited 7 times, 1 visits today)

LEAVE A REPLY

Please enter your comment!
Please enter your name here