Why should methods called from a class constructor be final? [duplicate]

风流意气都作罢 提交于 2020-12-08 07:15:46

问题


I'm a Java novice and I was trying to make sense of the following line from the tutorial at the Oracle website: https://docs.oracle.com/javase/tutorial/java/IandI/final.html

Methods called from constructors should generally be declared final. If a constructor calls a non-final method, a subclass may redefine that method with surprising or undesirable results.

I tried reading it multiple times trying to make sense of how a method called from a constructor can be redefined by a subclass. Should I assume the method being called by the constructor is one declared within the class of the constructor? And why should a method be declared final if it's called from within a constructor? (As opposed to from within a nested class or within another method?) I wasn't able to wrap my head around that statement. An example would be great.


回答1:


This is valid code (compiles):

class Person {
    Person() {
        init();
    }

    void init() {
        // do stuff
    }
}

class Employee extends Person {
    Employee() {
        super();
    }

    void init() {
        // do something else
    }
}

And it's highly suspicious. Because Person.init might do something critical for the integrity of the class, and there's no guarantees that Employee.init will do it too.

Restricting Person.init to private is not good enough. Employee.init remains valid, but it will shadow Person.init, which will be simply very misleading. It's best to make Person.init final, which make prohibit creating Employee.init.



来源:https://stackoverflow.com/questions/27661992/why-should-methods-called-from-a-class-constructor-be-final

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