By designating a class method with a modifier
final
, we mean that no derived class is able to override this method by changing its internal implementation. In other words, we are talking about the final version of the method. The class as a whole can also be marked as final
.
final class NoExtending {
// …
}
A class marked as final
is not inheritable, and all of its methods implicitly acquire the property final
. Using the a flag final
in class and method declarations can increase code security. If a class is given the modifier final
, no one is able to extend the class and probably break its contract in doing so. If final
a method is flagged, you can fully trust its internal implementation in all situations without fear of "fake". Appropriate to applyfinal
, for example, in the declaration of a method that provides for checking the password entered by the user to ensure the exact execution of what the method originally intended. A possible attacker will not be able to change the original implementation of such a method by slipping into the program its overridden version, which, say, always returns true, indicating successful user registration, regardless of what password he actually entered. You are free, if the situation permits, to go further and declare it as final
a class as a whole; the method ValidatePassword
will acquire the same property indirectly. The use of a modifier final
in the declaration of a method or class imposes serious restrictions on the possibility of further use and development of the code. Applicationfinal
in a method declaration is a sure indicator that the implementation of the method is self-contained and complete. Other programmers who want to take advantage of your class by extending its functions to suit their own needs will be constrained in their choice of means to achieve the goal, or completely deprived of them. By tagging final
a class as a whole, you will prevent it from being inherited, and probably significantly reduce its practical value to others. When you are about to apply the modifier final
, make sure that YOU are ready for such sacrifices and whether it is worth making them. In many cases, to achieve a sufficient level of code security, it is not at all necessary to mark the entire class as final
- it is quite possible to preserve the extensibility of the class by marking with the modifierfinal
only its "critical" structural elements. In this case, you will leave the main functions of the class intact and at the same time allow its inheritance with the addition of new members, but without redefining the "old" ones. Of course, the fields accessed by the method code final
must in turn be designated as final
either private
, because otherwise any derived class will be able to change their contents, influencing the behavior of the corresponding methods. Another effect of applying the modifier final
is related to the simplification of the code optimization problem solved by the compiler. Here is what happens when a method not marked as is calledfinal
, the runtime system determines the actual class of the object, associates the call with the most appropriate code from the group of overloaded methods, and transfers control to that code. But if, for example, the method getName
in the class example Attr
discussed earlier were designated as final
, the operation of accessing it would probably be noticeably simplified. In the most trivial case, like the one that concerns getName
, the compiler can simply replace the method call with its body code. This mechanism is called code embedding (inlining). When using the inline version of the method, getName
the following two expressions work exactly the same:
system.out.println("id = " + rose.name);
system.out.println("id = " + rose.getName());
Although the above expressions are equivalent, the second one still has the advantage of getName
allowing the name field to be read-only and the class code to have some degree of abstraction that allows the class implementation to be more freely modified. The same optimization scheme can be applied by the compiler to the private
and methods statiс
, since they also cannot be redefined. Using a modifier final
in class declarations also improves the efficiency of some type checking operations. In this case, many of these operations can be performed already at the compilation stage and therefore potential errors are detected much earlier. If the compiler encounters a class reference in the source textfinal
, it can be "sure" that the corresponding object is of the specified type. The compiler is able to immediately determine the place occupied by a class in the general class hierarchy, and check whether it is used correctly or not. If the modifier final
is not applied, the corresponding checks are carried out only at the program execution stage. Exercise 3.4. Is it advisable to include the final modifier in the method declarations (and if so, which ones) of the vehicle and passengervehicle classes ? Link to the original source: http://src-code.net/methody-i-klassy-final-java
GO TO FULL VERSION