final keyword has the following uses:
- If a variable is declared final, its value cannot be changed further in the program.
- If a method is declared final, that method cannot be overridden.
- If a class is declared as final, it cannot be inherited.
Program
//PROGRAM TO SHOW USE OF FINAL KEYWORD
final class A_final
{
public void display()
{
System.out.println(“display() of A_final”);
}
}
class B //if we extends A_final here then it will be a error
{
public final void show()
{
System.out.println(“show() of B”);
}
}
class Main extends B
{
final int x=5;
//public void show(){ } it will give error
public static void main(String ar[])
{
Main ob=new Main();
ob.show();
//ob.x++; it will give error
}
}