java final methods code example

Example 1: what is final in java

Final is used to apply restrictions on class, method, and variable.
The final class can't be inherited, final method can't be overridden,
and final variable value can't be changed. Final is a keyword

Example 2: java final meaning

private final String hello = "Hello World!";
/*
The keyword final states that the variable, method or class
associated will not have it's value changed.
*/

Example 3: java final modifier on method

public class Shape {
  private int numberOfVertices;
  
  //with final modifier,
  //this method cannot be overridden by subclasses
  public final int getNumberOfVertices() {
    return numberOfVertices;
  }
}

Tags:

Java Example