JavaRush /Java Blog /Random EN /Java final methods and classes
articles
Level 15

Java final methods and classes

Published in the Random EN group
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 Java Methods and Classes - 1
final class NoExtending {
// …
}
A class marked as finalis not inheritable, and all of its methods implicitly acquire the property final. Using the a flag finalin 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 finala 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 finala class as a whole; the method ValidatePasswordwill acquire the same property indirectly. The use of a modifier finalin the declaration of a method or class imposes serious restrictions on the possibility of further use and development of the code. Applicationfinalin 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 finala 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 modifierfinalonly 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 finalmust in turn be designated as finaleither 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 finalis 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 getNamein the class example Attrdiscussed 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, getNamethe 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 getNameallowing 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 privateand methods statiс, since they also cannot be redefined. Using a modifier finalin 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 finalis 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
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION