final keyword – JAVA

final keyword has the following uses:

  1. If a variable is declared final, its value cannot be changed further in the program.
  2.  If a method is declared final, that method cannot be overridden.
  3. 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

}

}

Leave a Reply

Your email address will not be published. Required fields are marked *