xxxxxxxxxx
// create a final class
final class FinalClass {
public void display() {
System.out.println("This is a final method.");
}
}
// try to extend the final class
class Main extends FinalClass {
public void display() {
System.out.println("The final method is overridden.");
}
public static void main(String[] args) {
Main obj = new Main();
obj.display();
}
}
xxxxxxxxxx
Final is used to apply restrictions on class, method, and variable.
The final class can't be inherited, final method can't be overridden,
and final variable value can't be changed. Final is a keyword
xxxxxxxxxx
You cannot extend a final class. If you try it gives you a compile time error.