Java ProgrammingLearn About Final Classes, Methods, and Variables in Java

Learn About Final Classes, Methods, and Variables in Java

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

final class CLASSNAME

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

public final class test1 
{
    test1()
    {
        System.out.println("New object created");
    }
}

Now, type the following line:

public class newtest extends test1

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:

final class test1 
{
    test1()
    {
        System.out.println("New object created");
    }
    public static void main(String[] args)
    {
        test1 testobj1 = new test1();
        test1 testobj2 = new test1();
    }
}

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:

class test
{
    final void method1()
    {
        System.out.println("Final Method Called");
    }
}

Now, let’s inherit the test class:

public class test2 extends test

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:

class test
{
    final void method1()
    {
        System.out.println("Final Method1 Called");
    }
    void method2()
    {
        System.out.println("SuperClass implementation for Method2");
    }
}

public class test2 extends test
{
    @Override
    void method2()
    {
        System.out.println("SubClass implementation for Method2");    
    }
    
    public static void main(String[] args) 
    {
        test2 t = new test2();
        t.method1();
        t.method2();
    }
}

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:

final int min_pass_length = 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

Exclusive content

- Advertisement -

Latest article

21,501FansLike
4,106FollowersFollow
106,000SubscribersSubscribe

More article

- Advertisement -