Java final classes and methods (translated from Java tutorials)

Source: Internet
Author: User

From http://www.cnblogs.com/ggjucheng/archive/2012/11/26/2789749.html

You can declare some methods of the class as final. Use the final keyword to declare the method, indicating that this method cannot be overwritten by the quilt class.
The object class does this. Some of its methods are final.

If the method of a class is the key to object state consistency, so its implementation cannot be changed, you may want to create a method of final. for example, you want to put getfirstplayer in the chessalgorithm class as final:

ClassChessalgorithm {EnumChessPlayer {White, Black }...FinalChessPlayer getfirstplayer (){ReturnChessPlayer. White ;}...}

 

The method called by the constructor should be declared as final. If a constructor calls a non-final method, a subclass may redefine this method, resulting in some surprising results.
Note that you can also declare the entire class as final. If a class is declared in this way, it cannot be inherited. Sometimes this feature is very useful, for example, create a constant string class.

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.