为什么一个finalclass不能被继承,而一个final方法却可以被继承?
Why can't a final class be inherited, but a final method can be inherited?
我对 类 和 methods 之间的“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");
}
}
错误:
cannot inherit from Final A
class Final extennds 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();
}
}
Why can't a final class be inherited, but a final method can be inherited?
为什么?因为 final
对 class 和方法有不同的意义。
为什么意思不同?因为这就是 Java 语言设计者选择设计语言的方式!
Java 中的 final
关键字具有三种不同的含义。
一个final
class无法扩展。
无法覆盖 final
方法。
一个final
变量初始化后无法赋值。
为什么他们决定使用 final
在不同的上下文中表示不同的事物?可能是因为他们不需要保留 2 或 3 个不同的关键字。 (事后看来,这可能不是最好的决定。然而,这是值得商榷的......而且争论它可能是浪费时间。)
值得注意的是,其他关键词在Java中有多重含义;例如,static
和 default
(在 Java 8 中)。
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 方法继承只允许访问子 class 中的 final 方法,但不允许覆盖。
final关键字用于限制程序员。
Final 关键字可以是 变量、方法 或 Class .
当我们使用final关键字时:
- 使用变量我们不能修改变量的值。
- 使用方法我们不能重写方法。
- 与 class 我们不能继承 class.
我对 类 和 methods 之间的“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");
}
}
错误:
cannot inherit from Final A class Final extennds 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();
}
}
Why can't a final class be inherited, but a final method can be inherited?
为什么?因为 final
对 class 和方法有不同的意义。
为什么意思不同?因为这就是 Java 语言设计者选择设计语言的方式!
Java 中的 final
关键字具有三种不同的含义。
一个
final
class无法扩展。无法覆盖
final
方法。一个
final
变量初始化后无法赋值。
为什么他们决定使用 final
在不同的上下文中表示不同的事物?可能是因为他们不需要保留 2 或 3 个不同的关键字。 (事后看来,这可能不是最好的决定。然而,这是值得商榷的......而且争论它可能是浪费时间。)
值得注意的是,其他关键词在Java中有多重含义;例如,static
和 default
(在 Java 8 中)。
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 方法继承只允许访问子 class 中的 final 方法,但不允许覆盖。
final关键字用于限制程序员。
Final 关键字可以是 变量、方法 或 Class .
当我们使用final关键字时:
- 使用变量我们不能修改变量的值。
- 使用方法我们不能重写方法。
- 与 class 我们不能继承 class.