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

岁酱吖の 提交于 2020-05-10 06:24:39

问题


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 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..

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();
    }
}

回答1:


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

Why? Because final means different things for classes and methods.

Why does it mean different things? Because that is how the Java language designers chose to design the language!

There are three distinct meanings for the final keyword in 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.

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.)

It is worth noting that other keywords have multiple meanings in Java; e.g. static and default (in Java 8).




回答2:


                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.



来源:https://stackoverflow.com/questions/35458367/why-a-final-class-cannot-be-inherited-but-a-final-method-can-be-inherited

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!