Java Final Class

Summary: in this tutorial, you will learn about the Java final class and how to use it to define classes that cannot be inherited.

Introduction to Java final class

In Java, a final class cannot be inherited. When you define a class as final, you’re stating that it is complete and should not be extended.

In practice, you often use the final classes to encapsulate functionality that should remain unchanged.

To create a final class in Java, you use the final keyword as follows:

final class MyFinalClass {
    // Class members and methods
}Code language: Java (java)

In this syntax, the MyFinalClass is final. If you attempt to extend it, you’ll get an error:

// Error: Cannot inherit from final 'Calculator'
class Subclass extends MyFinalClass {
}Code language: Java (java)

Java final class example

The following example shows how to define the Calculator class as a final class:

final class Calculator {
    public static double add(double a, double b){
        return a + b;
    }
    public static double subtract(double a, double b){
        return a - b;
    }

    public static double divide(double a, double b){
        return a / b;
    }

    public static double multiply(double a, double b){
        return a * b;
    }
}Code language: Java (java)

The Calculator class contains four static methods that perform basic mathematical operations including addition, subtraction, multiplication, and division.

You can use the final Calculator class throughout your application without the risk of accidentally extending it or modifying its behavior.

Java final class use cases

You use final classes when you want to ensure a certain classes of your codebase remain unchanged.

Here are some common use cases of the final classes:

  • Utility Classes: Classes that contain static utility methods often make good final classes because the utility methods should not be extended.
  • Immutable Classes: Classes representing immutable objects (objects whose state cannot be changed after creation) are often declared as final to guarantee their immutability.
  • Security: Final classes can be used to prevent tampering with critical code in security-sensitive applications.

Final classes in Java SE

Java standard library has many built-in final classes. These classes cannot be extended because if you do so, their intended use would now make sense.

For example, the String (java.lang.String) is a final class because the string is immutable. Therefore, you cannot make an extended version of the String class via inheritance like this:

// error
class MyString extends String {
}Code language: Java (java)

Another example of a utility class, which is a final class, is the Math class (java.lang.Math). The Math class provides a static method for performing mathematical operations is a final class:

public final class Math {
 // ...
}Code language: Java (java)

Summary

  • Define a class as final if you don’t want it to be extended.
  • Use Java final classes for utility or immutable classes.
Was this tutorial helpful ?