Java final keyword

Final is using for three purpose:

  1. final is used to prevent method overriding
  2. final is used to prevent inherit a class
  3. final is used to make a variable constant
  1. final is used to prevent overriding: A method declared as final can’t be overridden.

Because show() declared as final, it can’t be overridden in B. If  we attempt to do so, a compile time error will result.

2. final to prevent inheritance: Sometimes we wants to prevent a class from being inherited. To do this, the class declares as final. Declaring a class as final implicitly declares all its member as final.

3. final as constant: A variable can be declared as constant with the keyword final. It must be initialize while declaring. Once we declare a variable with keyword final it can’t be reinitialized in the program. It is similar to the const in c/c++.