java 为什么final类不能被继承,而final方法可以被继承?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35458367/
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 a final class cannot be inherited but a final method can be inherited?
提问by sravani.s
I have a big confusion in the usage of "final" keyword between classes and methods i.e.why final methods only support inheritance but not final classes
我对类和方法之间“final”关键字的使用有很大的困惑,为什么 final 方法只支持继承而不支持 final 类
final class A{
void print(){System.out.println("hello world");}
}
class Final extends A{
public static void main(String[] args){
System.out.println("hello world");
}
}
ERROR:
错误:
cannot inherit from Final A class Final extennds A{ FINAL METHOD IS..
不能从 Final A 类继承 Final 扩展 A{ FINAL METHOD IS..
class Bike{
final void run(){System.out.println("run");}
}
class Honda extends Bike{
public static void main(String[] args){
Honda h=new Honda();
h.run();
}
}
回答by Stephen C
Why a final class cannot be inherited but a final method can be inherited?
为什么final类不能被继承,而final方法可以被继承?
Why? Because final
means different thingsfor classes and methods.
为什么?因为对于类和方法final
意味着不同的东西。
Why does it mean different things? Because that is how the Java language designers chose to design the language!
为什么它意味着不同的东西?因为这就是 Java 语言设计者选择设计语言的方式!
There are three distinct meanings for the final
keyword in Java.
final
Java 中的关键字具有三种不同的含义。
A
final
class cannot be extended.A
final
method cannot be overridden.A
final
variable cannot be assigned to after it has been initialized.
一个
final
类不能扩展。甲
final
方法不能被重写。一个
final
变量不能分配给它已被初始化后。
Why did they decide to use final
to mean different things in different contexts? Probably so that they didn't need to reserve 2 or 3 distinct keywords. (In hindsight, that might not have been the best decision. However that is debatable ... and debating it is probably a waste of time.)
为什么他们决定final
在不同的上下文中使用不同的含义?可能是因为他们不需要保留 2 或 3 个不同的关键字。(事后看来,这可能不是最好的决定。然而,这是有争议的……争论它可能是浪费时间。)
It is worth noting that other keywords have multiple meanings in Java; e.g. static
and default
(in Java 8).
值得注意的是,其他关键字在Java中具有多重含义;例如static
和default
(在 Java 8 中)。
回答by Sagar Misal
public class ParentDemoClass {
public final void sayHello(){
System.out.println(" Hello from parent ... PARENT");
}
}
//Inheritance example to override final method from child class
public class ChildDemoClass extends ParentDemoClass{
/*When tried to override gives compiler error Cannot override the
final method from ParentDemoClass */
public void sayHello(){
System.out.println(" Hello from child ... CHILD");
}
}
//Inheritance example to access final method from child class
public class ChildDemoClass extends ParentDemoClass{
public void sayHelloFromChild(){
System.out.println(" Hello from parent ... CHILD");
sayHello();
}
public static void main(String as[]){
new ChildDemoClass().sayHelloFromChild();
}
}
//OUTPUT is :
Hello from parent ................................. CHILD
Hello from parent ................................. PARENT
Final method inheritance only allow access to the final method in child class but overriding is not allowed.
final 方法继承只允许访问子类中的 final 方法,但不允许覆盖。