Object-oriented (final keyword), final keyword

Source: Internet
Author: User

Object-oriented (final keyword), final keyword

   

/**
* Created by rabbit on 2014-07-21.
* Final is finally used as a modifier
* 1. Class, function, and variable can be modified.
* 2. Classes, functions, and variables modified by final cannot be inherited or overwritten
*
**/

   

class demo
{
    final int x = 5;
    final void show1()
    {
        System.out.println("show1 running");
    }
    void show2()
    {
        System.out.println("show2 running");
    }
}
class demo2 extends demo
{
    void show2()
    {
        System.out.println("show2 restart running");
    }
}
public class final_Demo {
    public static void main(String [] args)
    {
        demo2 d = new demo2();
        d.show2();
        System.out.println("final_demo running");
    }
}


In PHP object-oriented systems, where are the final keywords used?

It is generally used to prevent a method of the parent class from being overwritten.

Java final keyword explanation

Final means that the value cannot be changed for constants, for example, final int I = 100. The value of this I will always be 100. The variables are different, but the reference cannot be changed. For example, final File f = new File ("c: \ test.txt "); then this f must not be changed. If f itself has a method to modify the member variables, such as whether the variables are readable, modification is allowed. Image metaphor: A woman defines a final husband. Her career and income can be changed, but this woman will not change her husband.

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.