Java object-oriented basics _ final

Source: Internet
Author: User

Final:
Disadvantages of inheritance: it breaks the code encapsulation, and the appearance of final makes up for this drawback.
Final keywords:
1-final is a modifier that can be used to modify classes, methods, and variables;
2-The final modified class cannot be inherited
3-The final modifier method cannot be overwritten.
4-The variable modified by final is a constant, which can only be assigned once and must be assigned a value during definition.
Rule: www.2cto.com
The defined final constant must be expressed in uppercase letters and separated by underscores.
Instance 1:
Class Father01 {
Final String FATHER01_NAME = "James ";
Final int FATHER01_AGE = 20;
 
Final void show (){
// Age = 9; // Error !!! Age is defined as final. It has been assigned a value and cannot be assigned a value.
System. out. println ("name:" + FATHER01_NAME );
}
}
Final class Father02 {
}
Class Son01 extends Father01 {
// Public void show () {// Error !!! Methods defined by final cannot be overwritten.
//
//}
}
// Class Son02 extends Father02 {// Error !!! Classes defined by final cannot be inherited
//
//}

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.