Java Inheritance-final keyword usage

Source: Internet
Author: User

Final keyword features:

1. Can be used to modify variables, methods, classes.

2. The modified variable is a constant. Once assigned, it cannot be modified (constants are typically used in conjunction with the static keyword)

3. The modifier class cannot be inherited for this class 4. The modification method represents that the secondary method cannot be overridden

public class Finaldemo {public static void main (string[] args) {//TODO auto-generated method stubfinal Demoa DA = new Dem OA ();D emob DB = new Demob ()//da = new Demoa; If the final decoration is added, DA can no longer modify the reference Da.testa ();d B.testa ();}} /*final*/class demoa{//If Final is added, demob can not inherit demoapublic final int a = 1;public int b =2;public/*final*/void TestA () {/ /If Final is added, the Testa in the demob can not rewrite the testa//a++ in Demoa; Because the final modified variable A is a constant, it cannot be modified System.out.println (a);} public void Testb () {System.out.println (b);}} Class Demob extends demoa{public int a = 1;public int b =2;public void TestA () {System.out.println (a);} public void Testb () {System.out.println (b);}}

Java Inheritance-final keyword usage

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.