Final Methods and Classes
If a method is declared final, it may not be overridden in subclasses (opposite extreme to abstract, which must be overridden!)
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!!
}
- In places where the compiler can tell that a final method will be called, it can produce optimized code to avoid overheads of “late binding”.
A final class cannot be extended.