We have a class A that calls its own method F in the constructor.
Now there is a Class B that inherits a and overwrites method F.
Then, when constructor B calls the constructor A, does F in the constructor a refer to A: f or B: F?
In C ++, The polymorphism of function calls is mainly implemented through virtual tables. The virtual tables are not implemented before the constructor is completed, so there is no polymorphism at this time, therefore, the call is still a: f.
However, this is not the case in Java. It calls the subclass method, namely, B: f.
At this point, the C ++ performance should be more reasonable.
Therefore, in Java, if you guarantee that the method called is not a derived class method during the construction, you must call the private or final method. Because the final method will not be overwritten, and the private method is also. By default, the private method is actually final. overwriting the private method actually generates a new method.