Introduction to final, finalcutpro

Source: Internet
Author: User

Introduction to final, finalcutpro

About final

Final is used to modify methods and attributes to indicate special meanings. When modifying a method, the method cannot be overwritten. When modifying an attribute, the attribute cannot be changed. Here, the attribute is divided into objects and basic types. When modifying a basic type, the attribute cannot be assigned a value after the basic type is assigned, the modifier object indicates that this property cannot point to other objects (the reference remains unchanged), but the object to which it points can still be changed.

Since the final method cannot be rewritten, are there any mistakes in the following procedures?

class Base {    private final void method() {        System.out.println("In Base...");    }}class Sub extends Base {    public void method() {        System.out.println("In Sub...");    }}

Answer: Correct.

You can use final keywords to declare classes, member variables, and member methods. Once declared, the class, member variables, and member methods cannot be inherited, modified, or overwritten.

In this example, the base class method is private and invisible in the subclass. Therefore, the method with the same name in the subclass is no longer overwritten, which is equivalent to a new method name.

This situation is special. The book says that if a method in the subclass has the same method name and parameter signature as the method in its superclass, it means that the method in the subclass override in the superclass. This statement is not strict.

Additional tips:

The final method cannot be overwritten by the quilt class. The compiler transfers the final method to the embedded mechanism (directly inserts the method subject into the calling place), greatly improving the execution efficiency (about 50% ). Many methods in the API class, for example, the getClass method in the Object class is the final method.
Note: final cannot be used to modify the constructor.

 

References:

Http://www.cnblogs.com/hasse/p/5015475.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.