i

JAVA Complete Course

Using Final To Prevent Overriding & Inheritance

In Java programming language, by default we can override any method from parent class in its subclass. But there is also mechanism which helps us to prevent overriding methods in subclasses. This can be achieved by final keyword. If we try to do this, we will get compilation error.

We have same approach if we want to prevent inheritance. If we have any class and we want our class not to be extended by any other class, in other words, if we want not to have subclasses, we mark our class as final using final keyword. Like the previous case, if we try to do this, we will get compilation error.