Java modifier final

Source: Internet
Author: User

1. The final modifier class cannot have child classes or inherit classes.

When you define a class as final, it indicates that you do not intend to inherit the class, and maybe others do not. In other words, for some reason, you never need to make any changes to the design of this class, or for security reasons, you do not want him to have a subclass.

Because final cannot be inherited, methods in the class modified by final are implicitly formulated as fianl because you cannot overwrite them. You can add final to the method in the fianl class, but this does not make any sense.

If the variable in the final class is not defined as the final type, the variable value in the final class can be changed.

2. The final modifier method cannot be rewritten.

The instance method modified by final cannot be replaced in its subclass.

Static Methods Modified by final cannot be redefined and hidden in their subclasses.

There are two reasons for using the final method. The first reason is to lock the method to prevent any class that inherits it from modifying its meaning. This is out of design considerations:

The second reason is to ensure that the methods used in inheritance remain unchanged and will not be overwritten.

Http://blog.csdn.net/niguang09/article/details/6035813

Http://blog.sina.com.cn/s/blog_515015800100g97h.html

 

Http://wenku.baidu.com/view/5f69acf49e3143323968931e.html

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.