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

final Java methods and classes

Published in the Random EN group
By denoting 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 finalcannot be inherited and all its methods indirectly acquire the property final. Using the a feature finalin declarations of classes and methods can increase the level of code security. If a class is equipped with a modifier final, no one can extend the class and probably break its contract in the process. If a sign finaldenotes a method, you can fully trust its internal implementation in all situations without fear of "forgery". It is appropriate to use final, for example, in the declaration of a method that requires verification of the password entered by the user to ensure that 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 an overridden version of it, which, say, always returns the value true, indicating successful registration of the user, regardless of what password he actually entered. You have the right, if the specific situation allows, to go further and declare finalthe entire class as a class; the method ValidatePasswordwill acquire the same property indirectly. The use of a modifier finalin a method or class declaration imposes serious restrictions on the possibility of further use and development of the code. The use of finala method in a declaration is a sure indicator that the method implementation is self-contained and completely complete. Other programmers who want to use your class, expanding its functions to suit their own needs, will be limited in the choice of means to achieve their goal or completely deprived of them. By marking finala class as a whole, you will disable its ability to be inherited and will likely significantly reduce its usefulness to others. When you are about to use the modifier final, make sure whether 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 necessary to designate the entire class as final- it is quite possible to preserve the class's extensibility by marking finalonly its "critical" structural elements with a modifier. 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 code of the methods finalmust in turn be designated as finaleither private, since otherwise any derived class will be able to change their contents, affecting the behavior of the corresponding methods. Another effect of using a modifier finalis related to simplifying the code optimization problem solved by the compiler. This is what happens when a method that is 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 had been designated as final, the operation of calling it might have been noticeably simplified. In the most trivial case, such as the one concerning getName, the compiler can simply replace the method call with its body code. This mechanism is called code inlining. When using the inline version of the method, getNamethe following two expressions are executed exactly the same:
system.out.println("id = " + rose.name);
system.out.println("id = " + rose.getName());
Although the above expressions are equivalent, the second still has an advantage, since the method getNameallows you to give the name field a read-only property, and the class code to have a certain degree of abstraction, which allows you to more freely change the class implementation. The same optimization scheme can be applied by the compiler to the privateand methods statiс, since they also do not allow overriding. Using a modifier finalin class declarations also makes some type checking operations more efficient. In this case, many such operations can be performed already at the compilation stage and therefore potential errors are detected much earlier. If the compiler encounters a reference to a class in the source text final, it can be "sure" that the corresponding object is of the type specified. The compiler is able to immediately determine the place occupied by a class in the overall 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/metody-i-klassy-final-java
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION