final modified class
When Hero is modified to final, it means that Hero cannot be inherited and
its subclasses will have compilation errors.
public final class hero extends Object{
String name;
int hp = 100;
}
final modification method
Hero's useItem method is modified to final, then this method cannot be overridden in ADHero
public final void useitem() {
System.out.println("hero use item");
item a = new item();
a.effect();
}
final modifies basic type variables
Final modifies the basic type variable, which means that the variable has only one chance to assign a value to
line 16, and line 17 can no longer be assigned.
public static void main(String args[]) {
final int hp;
hp = 100;
}
final modified reference
public static void main(String args[]){
final hero a;
a = new hero();
}
constant
public class test extends Object{
final int mp = 200;
}
Exercise-final
design a class SomeString, inherit the String class. Can it be inherited?
Answer: No! !