The role and difference of the final keyword in Java and C + +

Source: Internet
Author: User

the role of the final keyword in Java :

A.acting on a class : It is not allowed to extend the class, that is, you cannot define subclasses with that class.

b. Acting on methods : Subclasses cannot override this method, but can be inherited.

in early Java, programmers used final to avoid the overhead of dynamic binding. Now this function is mainly implemented by compilers.

C.action on domain : The value of the field cannot be changed after the object is constructed.

D. function parameter : can read, but cannot change the parameter.

Note:all methods of the final class automatically become final, excluding fields.

The primary purpose of declaring a method or class as final is to ensure that they do not change semantics in subclasses.

The string Class is a final class and does not allow a subclass of string to be defined. If there is a reference to a string, it must refer to a string object, and it cannot be an object of another class.

The final keyword in C + + is new in C + +11 and acts as:

A.acting on a class: Prevents a class from becoming a base class, that is, it is not allowed to extend the class.

b. Acting on virtual methods : The organization derived class overrides this virtual method.

The role and difference of the final keyword in Java and C + +

Related Article

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.