Final Methods and Classes
If a method implementation is declared final, it is not allowed to be overridden in subclasses (opposite extreme to abstract!)
If we declared draw() in Rectangle to be final, we could never give a more specialized draw() in a subclass:
class Rectangle extends Shape {
final void draw() {. . .} // final method
double height, width ;
}
class Square extends Rectangle {
void draw() {. . .} // Compile-time error!!
}
If the compiler can tell that a final method will be called, it can produce optimized code to avoid overheads of late binding. (“Non-virtual” or even “inline” method.)
A final class cannot be extended at all.