by Dinesh Thakur Category: Java Language

There are times when you want to prevent inheritance. Perhaps there is no need the extensibility for various reasons. If you know this for sure, there are advantages to declaring the final keyword precisely that.

Java has a special keyword called final that can use for creating final methods, final classes, and final variables. The final modifier is void, which simply means that no value is returned.The final modifier indicates that the value of this member cannot change. We get a compile-time error if our program tries to change its contents. Each of these is explained in more detail next.

Final Methods

Final methods are methods whose implementations cannot be changed. When a method declared as final, it cannot be overridden by a subclass method. In private methods is equal to final, but in variables, it is not. For example, consider a class E that contains a final method called computeSquare that computes the square of its argument:

public class E {
    public final int computeSquare(int x) {
         return x*x;
    }
}

By declaring computeSquare as fina1, we ensure that it not be overridden in a subclass. Thus, if subclass F attempts to override computeSquare, it causes a compilation error:

public class F extends E {
       // error: cannot override the final method computeSquare in E
       public int computeSquare(int x) {
              // some code goes here
       }
}

final keyword in java

Final Classes

There are times when you want to prevent inheritance. Perhaps there is no need to extend the class, for instance. If you know this for sure, there are advantages to declaring this class final.

Just like final methods, a class can also be declared final. A final class is a class that cannot use as a superclass (i.e., a Class cannot extend final class). To declare the class as final, precede the class header with the final keyword. So the class header looks like below.

final class ClassName {
     ..............
}

When you declare a class to be final, all methods of this class are implicitly final. It is because another class cannot extend the final class so no class can be in a position to override any of the methods in the final class. For example, String and StringBuffer classes are examples of final classes.

public final class HourlyEmployee extends Employee {
     // class definition
}

A class can declare as final for either of the following two reasons:

1. None of the methods in the class should override.
2. The class should not extend further.

The two main advantages of declaring final classes are higher efficiency and safety. The dynamic binding inherent in virtual methods is harder on the compiler, resulting in slower execution. By declaring a class final, virtual methods are replaced by the compiler with in-line code, speeding up the process. Final classes are also safer, in that they avoid ambiguous situations where a message sent may end up at an object that returns the wrong data.

Examples of final classes in the Java API are:

• An array is a collection of similar elements. The array is a predefined class present in java.lang package. It is a final class which can't be inherited.

• In java String is a predefined class present in java.lang package. A string is a final class, and it is immutable.

• Each of the numeric type-wrapper classes - Byte, Short, Integer, Long, Float, and Double extends class Number. Also, the type-wrapper classes are final classes, so you cannot extend them.

• The System class provides a system-independent programming interface for accessing such resources outside the Java environment. The System class should not instantiate. It is a final class, and all its variables and methods are static.

• The java.sql.Numeric class adds the capability of representing SQL fixed point NUMERIC and DECIMAL values to the java.lang.Number class. The Numeric class is a final class and, therefore, cannot be redefined.

Final Variables

Final variables are declared using the final keyword. A final variable can be assigned a value only once. A variable that is declared as final and not initialized is called a final blank variable. A final blank variable forces the constructors to initialize it. Java local classes can only reference local variables and parameters that declared as final. It declares a variable called ANGLE:
final int ANGLE = 10;
Reassigning to ANGLE again results in an error:
ANGLE = 20; // error, ANGLE has been already assigned.
Final variables are useful in local and anonymous classes.

How do final variables differ from constants? Constants are class variables because they declared with the static modifier. It means that a single copy of this variable shared among all instances of a class. Final variables, on the other hand, are instance variables.

A distinct advantage of declaring a java variable as static final is the compiled java class results in faster performance.

final’ should not call as constants. Because when an array declared as final, the state of the object stored in the array can be modified. You need to make it immutable in order not to allow modifications. In a general context, constants do not allow modifying. In C++, an array declared as const not allow the above scenario, but java allows. So java’s final is not the general constant used across in computer languages.

A variable that is declared static final is closer to constants in general software terminology. You must instantiate the variable when you declare it static final.





About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.