Final keyword in java


  • Final keyword can be used:
    1. with class name
    2. with method name
    3. with variable name
  • Uses of final Keyword:
    1. To prevent Inheritance: If we want to prevent a class from being inherited then we use final keyword.
    2. To prevent Overriding: If we want that method should not be overriden by sub class then we use final keyword..
    3. To make the value of variable as constant: If we make the variable final then this will be treated as constant its value remains unchanged.
  • Example:
    1. final class A { ------------ } class B extends A//cant extend error { ----------- }
    2. class A { final void finalmeth() { ----------- } } class B extends A { Void finalMeth()//error cant override { ------------ } }
    3. final int a=10;