final class java

Use of final class in Java

A final keyword means the class cannot be subclassed. In other words, no other class can ever extend a final class. And many of the Java standard library classes are final, such as java.lang.System  and java.lang.String .

When you would mark a class final ?

You should make a final class only if there is need for you that none of the methods in that class should be overridden i.e., when you need security so that nobody could change the implementation that you made.

Marking your class final also means that the class you wrote will never be improved upon by any other programmer.

package com.sneppets.corejava;

public final class Vehicle {
	public void move(){
		System.out.println("Vehicle Moving");

Now let’s try to compile the following Car subclass

package com.sneppets.corejava;

public class Car extends Vehicle{ }

You would see error like this

“The type Car cannot subclass the final class Vehicle”

Benefits of having nonfinal classes

Let’s say there is a problem with a method in a final class that you have written. And another programmer who is using your code don’t have the source code. So that programmer can’t modify the source to improve the method.

If the class in non-final then the programmer can extend the class and override the method in his new subclass. If the class is final then he is stuck.


A final class will loose the main benefit of OO -extensiblity. In practice you will never make a final class. Unless you have a serious safety or security issue then don’t mark a class final.

Notify of

Inline Feedbacks
View all comments