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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 00:10:06  来源:igfitidea点击:

Why a final class cannot be inherited but a final method can be inherited?

javaclassinheritancefinal

提问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 finalmeans 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 finalkeyword in Java.

finalJava 中的关键字具有三种不同的含义。

  • A finalclass cannot be extended.

  • A finalmethod cannot be overridden.

  • A finalvariable cannot be assigned to after it has been initialized.

  • 一个final类不能扩展。

  • final方法不能被重写。

  • 一个final变量不能分配给它已被初始化后。

Why did they decide to use finalto 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. staticand default(in Java 8).

值得注意的是,其他关键字在Java中具有多重含义;例如staticdefault(在 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 方法,但不允许覆盖。