[英]why use final as modifier when we have private in method classes
Let's consider that we have a method which has been declared as below: 让我们考虑一下我们有一个如下声明的方法:
final private method myMethod() {
}
Why should we use final
when we have private
as a modifier. 当我们将
private
作为修饰符时,为什么要使用final
。 What exactly is the purpose of final here? 最终在这里的目的是什么?
final
prevents a method from being overriding in any subclass. final
防止方法在任何子类中被覆盖。 But it is still accessible from other classes. 但是仍然可以从其他类中访问它。
private
makes a method inaccessible from any other class. private
使方法无法从任何其他类访问。
So make a method final
when you want to make sure that no child class overrides(changes the implementation) this method. 因此,当您要确保没有任何子类覆盖(更改实现)此方法时,请使该方法成为
final
方法。
Make it private
when you want that no other class should be able to use it directly. 当您希望其他任何类都不能直接使用它时,请将其设为
private
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.