Final keyword in java
- Final keyword can be used:
- with class name
- with method name
- with variable name
- Uses of final Keyword:
- To prevent Inheritance: If we want to prevent a class from being inherited then we use final keyword.
- To prevent Overriding: If we want that method should not be overriden by sub class then we use final keyword..
- 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:
- final class A
{
------------
}
class B extends A//cant extend error
{
-----------
}
- class A
{
final void finalmeth()
{
-----------
}
}
class B extends A
{
Void finalMeth()//error cant override
{
------------
}
}
- final int a=10;