Java final keyword Tutorial

In this section, we will learn what the final keyword is and how to use it in Java.

Note: before reading this section, we’re assuming you’re already read the final keyword and variables in Java.

What Is Final Keyword in Java?

Depending on where we use the `final` keyword, it has these effects:

  • If we use the `final` keyword on variables, it will turn those variable into immutable. That means after initialization, we can’t change the value of the target variable anymore.
  • If we use the `final` keyword on classes, the target class can no-longer be used for inheritance. Basically, other classes can’t use it as the base class.
  • If we use the `final` keyword on methods, that method cannot be overridden anymore. (Check the method overriding section to learn more).

The use of final keyword in Java

The final keyword can be used for:

– Classes

– Variables

– Methods

Now let’s see how to use them in these areas.

Java `final classes`:

A class that is declared as final cannot be used to inherit from.

Note: the class still can inherit from other classes but it can’t be the base or the parent of another class anymore

Example: creating final classes in Java

public final class Parent {
//body...
}

public class Child extends Parent {
//body...
}

Output: Error:(3, 28) java: cannot inherit from final tuto.Parent

As you can see, because the `Child` class inherits from a final class `Parent`, we got compile time error with the message that you see in the output.

Java `final methods`:

Sometimes we want to use a class as the base class and allow other classes to inherit from but there’s a method in that class that we don’t want other derived classes to override!

This is where we can turn that method in the base class into final and that signals the compiler that no child class can override this method.

Note: of course, the child classes can access the method and use it but they just can’t override it.

Example: creating final methods in Java

public class Parent {
    
    public final void callMe(){
        //body...
    }
}
public class Child extends Parent {

    @Override
    public void callMe(){
        //body...
    }
}

Output:

Error:(6, 17) java: callMe() in tuto.Child cannot override callMe() in tuto.Parent

overridden method is final

In this example, the `Child` class inherits from the `Parent` class. So far because the `Parent` class is not set as the `final` class, we don’t have a problem.

But the error arises when we attempt to override the method named `callMe()` in the `Child` class.

Basically, this method is a final method, and that means no child class is allowed to override it. But here we did and for that we got the compile time error.

Java `final variables`:

This is explained in final variables section.

FAQ:

Can we use `const` keyword in Java?

Java does not support the keyword `const`. So for those of you with JavaScript or C# background, you should know that the `final` is the equivalent of `const` keyword.

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies