Final Methods
A final method can't be overridden by subclasses. This is used to prevent unexpected behavior from a subclass altering a method that may be crucial to the function or consistency of the class.
Example:
public class MyClass { public void myMethod {...} public final void myFinalMethod {...} } public class AnotherClass extends MyClass { public void myMethod {...} // Ok public final void myFinalMethod {...} // forbidden }A common misconception is that declaring a class or method final improves efficiency by allowing the compiler to directly insert the method inline wherever it is called. In fact the compiler is unable to do this because the method is loaded at runtime and might not be the same version as the one that was just compiled. Only the runtime environment and JIT compiler have the information about exactly which classes have been loaded, and are able to make better decisions about when to inline, whether or not the method is final.
Read more about this topic: Final (Java)
Famous quotes containing the words final and/or methods:
“After the final no there comes a yes
And on that yes the future world depends.”
—Wallace Stevens (18791955)
“I think it is a wise course for laborers to unite to defend their interests.... I think the employer who declines to deal with organized labor and to recognize it as a proper element in the settlement of wage controversies is behind the times.... Of course, when organized labor permits itself to sympathize with violent methods or undue duress, it is not entitled to our sympathy.”
—William Howard Taft (18571930)