Classes that cannot be extended are called final classes, and you use the final modifier in the definition of the class to indicate this.

Methods that cannot be overrided by subclass are called final methods.

⚠️ All methods and only the methods in a final class are automatically final. 其实仔细想想,final field 和 final method 的味道还是有点不一样的

When to Use Final Term

确保这个玩意的含义不能在子类中修改了。这也说明了一个 final class 的 obj variable 我们能够确定它一定指向这个类,而不是子类

对于有些程序员 (可能是来自别的语言的习惯) 会让所有的方法都是 final,只有真的想让它可重写才去掉 (用书中的话:unless you have a good reason to want polymorphism.)

另外,使用 final 当方法很短的时候,还能触发 inline。如果 override 了,那编译器就不知道会是啥,就不能 inline 了 (但是其实虚拟机里的编译器,不管什么情况都能智能的 inline 方法,所以这些东西初学还是别操心了)