Java 为什么我们不能覆盖静态和最终方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20282027/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why is it that we cannot override static and final methods?
提问by
I am trying to understand why we can't override static and final methods. I do not get the purpose behind it.
我试图理解为什么我们不能覆盖静态和最终方法。我不明白它背后的目的。
回答by Domi
回答by Joachim Sauer
final
methods can't be overriden, because that's what final
is designed to do: it's a sign saying "do not override this".
final
方法不能被覆盖,因为这final
就是设计的目的:这是一个标志,说“不要覆盖这个”。
static
methods can't be overriden, because they are never called in a polymorphic way: when you call SomeClass.foo()
it will alwaysbe the foo
method of SomeClass
, no matter if there is another ExtendedSomeClass
that has a much groovier foo
method.
static
方法不能被覆盖,因为它们永远不会以多态的方式被调用:当你调用SomeClass.foo()
它时,它将始终是 的foo
方法SomeClass
,无论是否有另一个ExtendedSomeClass
具有更常规foo
方法的方法。
回答by Nick Div
Final cannot be overridden because that is the purpose of the keyword, something that cannot be changed or overridden.
Final 不能被覆盖,因为这是关键字的目的,不能改变或覆盖的东西。
The purpose of inheritance and polymorphism is to have objects of same class implementing methods (not the names but the code in the methods) in different ways. And static methods cannot be accessed by objects because they are a part of the class not the instance. So there is no purpose to overriding the Static methods. And you can have a static method in subclass by the same name but that won't be an overridden method.
继承和多态的目的是让同一类的对象以不同的方式实现方法(不是名称,而是方法中的代码)。静态方法不能被对象访问,因为它们是类的一部分而不是实例。所以没有覆盖静态方法的目的。并且您可以在子类中使用同名的静态方法,但这不会是重写的方法。
If you haven't yet read about Inheritance and Polymorphism which are both features of Java, you should and also try to write the code in the question so that SO users can answer with an example.
如果你还没有读过继承和多态这两个 Java 的特性,你应该并且也尝试在问题中编写代码,以便 SO 用户可以用一个例子来回答。
回答by Rahul Tripathi
The reason for not overriding static method is that Static methods are treated as global by the JVM so there are not bound to an object instance at all. Similarly final methods cant be overriddent because when you say a method as final then it mean you are saying to the JVM that this method cannot be overridden.
不覆盖静态方法的原因是静态方法被 JVM 视为全局方法,因此根本没有绑定到对象实例。类似地,final 方法不能被覆盖,因为当你说一个方法为 final 时,这意味着你对 JVM 说这个方法不能被覆盖。
The wikihas a very important misconception about final. Do read that!
在维基大约有最后一个非常重要的误解。一定要读!
A final method cannot be overridden or hidden by subclasses.[2] This is used to prevent unexpected behavior from a subclass altering a method that may be crucial to the function or consistency of the class.[3]
A common misconception is that declaring a class or method as final improves efficiency by allowing the compiler to directly insert the method wherever it is called (see inline expansion). But because the method is loaded at runtime, compilers are unable to do this.Only the runtime environment and JIT compiler know exactly which classes have been loaded, and so only they are able to make decisions about when to inline, whether or not the method is final.[4]
final 方法不能被子类覆盖或隐藏。 [2] 这用于防止子类更改可能对类的功能或一致性至关重要的方法的意外行为。 [3]
一个常见的误解是,将类或方法声明为 final 可以提高效率,因为它允许编译器在调用方法的任何地方直接插入该方法(请参阅内联扩展)。但是由于该方法是在运行时加载的,编译器无法执行此操作。只有运行时环境和 JIT 编译器知道哪些类已被加载,因此只有它们能够决定何时内联,无论该方法是否为 final。[4]
回答by uhs
final
is used to avoid overriding. And a static method is not associated with any instance of a class so the concept is not applicable.
final
用于避免覆盖。并且静态方法不与类的任何实例相关联,因此该概念不适用。