DEV Community

Bharath kumar
Bharath kumar

Posted on

final Keyword

"final" is a keyword used to restrict modifications to variables, prevent methods from being overridden, and disallow inheritance for classes.

You use the final keyword in a method declaration to indicate that the method cannot be overridden by subclasses.

Final keyword has a numerous way to use:

A final class cannot be subclassed.
A final method cannot be overridden by subclasses
A final variable can only be initialized once
Enter fullscreen mode Exit fullscreen mode
final class Human{
  public String Name="john";
   final void display(){
        final int a=10;
        System.out.println("My name is : "+ a);

  }
   public void getage() {
         int age=25;
     System.out.println("My age is: "+ age);        
    }

}
Enter fullscreen mode Exit fullscreen mode
class Men extends Human{
    @Override
     public void display() {
        System.out.println("this is the display");
    }

    public static void main (String []args) {

         Men obj = new Men();
         obj.display();
         obj.getage();

    }
Enter fullscreen mode Exit fullscreen mode

Error:because cannot extend the final class to another class
it is used in another class to object created...final methods cannot overrides and final variables cannot be changed.....

Top comments (0)