final Class in java

A final class in Java is a class that is declared with final keyword. The final class in Java cannot be subclassed. This means that no other class can extend the final class and inherit its properties and methods. For example, consider the following final class:

final class Point {
	private int x;
	private int y;

	public Point(int x, int y) {
		this.x = x;
		this.y = y;

	public int getX() {
		return x;

	public int getY() {
		return y;

In this example, the Point class represents a point in two-dimensional space and has x and y coordinates. Since the Point class is marked as final, it cannot be subclassed. The following code does not compile:

class ColoredPoint extends Point { // This line would cause a compilation error
	private String color;

	public ColoredPoint(int x, int y, String color) {
		super(x, y);
		this.color = color;

	public String getColor() {
		return color;

Final classes are used to represent concepts that are fundamental and cannot be extended or changed. For example, the java.lang.String class is a final class because it represents a string of characters and there is no need to allow other classes to extend it.

There are several benefits to using final classes in Java. Firstly, final classes can be more efficient because the Java Virtual Machine (JVM) can optimize them more aggressively. This is because the JVM knows that no other class can override the methods of a final class, so it can safely inline the method calls and eliminates virtual method dispatch.

Another benefit of final classes is that they provide a higher level of security. Since final classes cannot be subclassed, it is not possible for malicious code to extend a final class and override its methods in order to compromise the security of the application. This makes final classes particularly useful for classes that contain sensitive information or critical functionality.