简体   繁体   English

为什么final类不能被继承,而final方法可以被继承?

[英]Why can't a final class be inherited, but a final method can be inherited?

I have a big confusion in the usage of the "final" keyword between classes and methods.我对在类和方法之间使用“final”关键字有很大的困惑。 Ie, why do final methods only support inheritance, but not final classes?即,为什么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();
    }
}

Why can't a final class be inherited, but a final method can be inherited?为什么final类不能被继承,而final方法可以被继承?

Why?为什么? Because final means different things for 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. Java 中final关键字有三种不同的含义。

  • A final class cannot be extended. final类不能扩展。

  • A final method cannot be overridden. final方法不能被覆盖。

  • A final variable cannot be assigned to after it has been initialized. final变量在初始化后不能赋值。

Why did they decide to use final to mean different things in different contexts?为什么他们决定在不同的上下文中使用final来表示不同的东西? Probably so that they didn't need to reserve 2 or 3 distinct keywords.可能是因为他们不需要保留 2 或 3 个不同的关键字。 (In hindsight, that might not have been the best decision. However that is debatable ... and debating it is IMO a waste of time.) (事后看来,这可能不是最好的决定。然而,这是有争议的......而且辩论它是 IMO 浪费时间。)

It is worth noting that other keywords have multiple meanings in Java;值得注意的是,其他关键字在 Java 中具有多重含义; eg, static and default (in Java 8).例如, staticdefault (在 Java 8 中)。

Final keyword is used to restrict the programmer. final 关键字用于限制程序员。

A Final keyword can be a Variable , Method or Class . Final 关键字可以是变量方法

When we use the final keyword:当我们使用 final 关键字时:

  • with variable we cannot modify the value of the variable.使用变量我们不能修改变量的值。
  • with method we cannot override the method.使用方法我们不能覆盖该方法。
  • with class we cannot inherit the class.我们不能继承类。
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 the child class, but overriding is not allowed. final 方法继承只允许访问子类中的 final 方法,但不允许覆盖。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM