Your Class is Complete and Provides Special Unique Functionality . Mark as “final”


final method

Java encourages use of OOPS features such as inheritance and encapsulation. But in special cases, we want to restrict the change of our class or method or member variables or local variables. Here “final” keyword helps to mark the element initialized only once or design once and mark as complete.  It is not allowed to create subclass from final class.

Your class is complete and no further enhancement is required.  Mark as “final”.

Let us take an example, you have written class “LoginInfo”.

class LoginInfo{

String username;
String password;

//All constructor,getter and setters.

public void setPassword(String password){

// Your encryption logic to save the password in encrypted form.

}

public String getPassword(){

// Your decryption logic to return the password by decrypting it..

}

}

LoginInfo has username and password member variables. Managing the username and password without encryption, would make  security issue and  security violation.  So you analyzed best security and encryption algorithm to hide password to the external world.  Your code worked fantastic. Great!! you have done great job. After few months, a new team member joins your team and he is not aware of your fantastic implementation of your encryption algorithm.  He creates new class as “Credentials” by extending the class “LoginInfo”.

class Credentials extends LoginInfo{

public void setPassword(String password){

// no encryption logic; simply save the password in DB.

}

public String getPassword(){

// No decryption logic:

}

}

New team member changed the setPassword and getPassword without encryption and decryption.  He started using “Credentials” instead of “LoginInfo”. All your effort and complex encryption algorithm became useless.

Do not allow your effort and complex logic to be lost by your mistake. Mark  as “final”.

So how to avoid this kind of real time scenario in our application. Mark your class as “final”.  But ensure your class has some special unique behaviour.  In the above LoginInfo class has encryption and decryption logic as unique feature.  So mark that as “final” makes sense.  It will resolve all the above discussed issue in application.

Mark as final if it is unique. Do not mark all the classes as final. It is worst design.

Java API has final classes such as String, StringBuffer, Math and wrapper classes.

String :  It is complete implementation for string variables. The best class definition and methods implemented for String class. It written to give better performance and proper functional logic. So overwriting any of the methods would create issues and performance impact in the code. So it has been marked as “final”. The same applicable to String, StringBuffer and Utility class Math.

Math : It is complete implementation for standard mathematical operation. It provides properly implemented methods for exponential, square, minimum, maximum, log. All the method are implemented properly and no future enhancement is required. So marking this as “final” would avoid creating child class from Math.

Utility class can be marked as “final” as it provides properly implemented helper methods like Math.java

Final is a keyword or reserved word in Java and can be applied to member variables, methods, class and local variables in Java. If it marked as final, it can’t be allowed to change that reference and compiler will verify this and raise a compilation error if you try to re-initialize final variables in Java.

Final class : Marking class as final would restrict creating subclass from the class. If the class is complete and proper itself, why should it be allowed to create subclass from that class.  So mark that as final.

Final methods: Marking methods as final would not allow to overwrite the method in subclasses.  Here class is not final, only methods are final. So subclasses can be created from the class, but the final methods in the class are not allowed to override in subclass.  Same final class intention for method as well. Your method involves unique and complex logic, mark it as final to avoid further refinement of the methods.

Final member variables: In our class, if assign value to member variables, it should not be changed or reassigned to different value. For example, Employee ID in the Employee class, it shouldn’t be changed once it assigned. Employee always referred by emp ID and by only one employee ID, by the same emp ID.  Final member variables can be assigned value in constructor or in declaration section. Assigning value to member value is not even allowed in setter and clone() methods.

Final members can be set in constructor or in declaration section.

class Employee{
public final String empID;
}

Final variables: Final variables are set for local variables or static variables such as constants.

public static final double PI = 3.14;
public static final int THOUSAND = 1000;

The naming convention for final constants is variable name should be in CAPS like PI,THOUSAND. Marking final ensuring modifying the standard value for the meaningful variables.

I hope now we are clear about usage of “final” keyword in class and methods. “final” keyword can be used for method argument as well. We would discuss final in method argument in details in next post. Happy finalizing!!!:) Kindly share your comments on final.

You may also like