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:
“There is no country in which so absolute a homage is paid to wealth. In America there is a touch of shame when a man exhibits the evidences of large property, as if after all it needed apology. But the Englishman has pure pride in his wealth, and esteems it a final certificate. A coarse logic rules throughout all English souls: if you have merit, can you not show it by your good clothes and coach and horses?”
—Ralph Waldo Emerson (18031882)
“I conceive that the leading characteristic of the nineteenth century has been the rapid growth of the scientific spirit, the consequent application of scientific methods of investigation to all the problems with which the human mind is occupied, and the correlative rejection of traditional beliefs which have proved their incompetence to bear such investigation.”
—Thomas Henry Huxley (182595)