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.