When a METHOD is declared with final keyword, it is called a final method.
A final method cannot be OVERRIDEN.
when a CLASS is declared final then it cant be INHERITED.
when a VARIABLE is declared final its value cant be CHANGED.
public class Counter {
private int counter = 0;
public final int count() {
return counter++;
}
public final int reset() {
return (counter = 0);
}
}
class FinalDemo {
// create a final method
public final void display() {
System.out.println("This is a final method.");
}
}
class Main extends FinalDemo {
// try to override final method
public final void display() {
System.out.println("The final method is overridden.");
}
public static void main(String[] args) {
Main obj = new Main();
obj.display();
}
}