The final related stuff in Java

Source: Internet
Author: User

Final is a keyword in Java, previously thought C + + does not have this keyword, just checked, unexpectedly! Then Also Yes! It seems that the usage is similar.

Roughly divided into so many, by this thing modified the east, so far, can not be changed.

For example

1 function is modified, cannot be overwritten.

2 classes are modified, they cannot be inherited.

3 variables are modified, can only be assigned once, remember Oh, this and C + + static const is not the same, this thing can be declared, not necessarily to declare when the assignment, but only! Yes! Fu! Value! One! Times!.

On the Code

Final class  animal{//Add here, the following inheritance will error OH public final  int ff;  That's it. {ff= 4;//You can assign a value with a code block}public Animal (int t) {//this.ff= t;//can also be assigned with a constructor System.out.println ("Create Animal");//TODO Auto-generated constructor stub}public final void Cry () {//Added here, the following cover also does not have the bird System.out.println ("wo~~~~~~~");} Public final void Run () {//But don't forget, although not rewritten, the System.out.println ("Runrunrunrunr") can be used as a make-up.}} Class Dog extends Animal{public dog (int t) {super (T); System.out.println ("Create Dog");//TODO auto-generated constructor stub}public void Cry () {run (); System.out.println ("wang~~~~~~" +ff);} Class Test{public static void Main (string[] args) {Dog dog = new Dog (3);d og.cry ();D og dog2 = new Dog (5);d og2.cry ();}}

That's it, the rest.

The final related stuff in Java

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.