Learn About Final Classes, Methods, and Variables in Java

0
1361
Java-17

Java-17

In previous articles, we have talked about Abstract Classes, which are classes that can’t be instantiated, and must be inherited. We also covered Method Overriding, which means a method implementation in a child class overrides the method with the same name in its parent class. In this article, we are going to tackle the opposite concepts: Final Classes, and Final Methods. We will also use the same keyword final (used with final classes and methods) to nominate some variables as constants. Have a nice reading.

Final Classes
A final class is a class that cannot be inherited.

Syntax

Example
In your NetBeans IDE, open a new Java source file, and type the following code:

Now, type the following line:

As soon as you finish typing this line, you will get an error notification that warns you, telling that the new class can’t inherit from the final class test1.
1

If you modify the code:

and run it, it should compile and run without errors.

Final classes allow you to prevent others from inheriting your classes, and to enforce them to use those classes as they are.

Final Methods
A final method can’t be overridden in any subclass. This is useful when you need to ensure your implementation for the method will be used without alteration or disabling.

Example
In a similar way, consider class test that has a final method:

Now, let’s inherit the test class:

Attempting to re-define method1() within the subclass should give an error notification like the following one:
2

The error clearly states that method1() in the subclass can’t override the original method1() in the parent class, because it was declared as final.

Now, re-write the entire program as follows:

When executed, this program will give the following output:
3

The object t has called method1() and method2(). Being final enforces one unique implementation for method1(). So, when called from an object of the subclass, the superclass version gets invoked and executed. On the other hand, the non-final method method2() has been overridden in the subclass. So, when called from the t object, the subclass version is executed.

Java Programming Course for Beginner From Scratch

Final Variables
To mark a variable as read only (i.e. constant), declare it as a final variable. Once declared and initialized, a final variable can’t be altered.

Note
A final variable should be initialized. Otherwise, it will be meaningless!

Example
The following statement will define the constant min_pass_length, and give it a value of 8:

Summary

  • A final class is a class that can’t be extended.
  • A final method is a function in the base class that can’t be overridden by any subclass.
  • A final variable is considered read only. Once defined and initialized, it can’t be modified. This is useful when defining constants.
  • To mark a class, method, or variable as final, use the final keyword in its declaration.

I hope you find this article useful. See you again.

LEAVE A REPLY

Please enter your comment!
Please enter your name here