The final key word can be used on the following:
-
classes
-
methods
-
parameters
You can use the keyword whenever you want to make sure that no one will override or change your classes, methods, or parameters.
Example final class – the class can not be inherited.
public final classs book extends Product { // all methods in the class are automatically final }Example final method – the method can not be overriden
public final String getVersion() { return version; }Example final parameter – prevent a method from assigning a new value to a parameter.
public void setVersion(final String version) { // version = "new value"; // not allowed this.version = version; }