Calling an overridden method from a constructor

后端 未结 4 949
滥情空心
滥情空心 2021-02-15 09:22

In the following example:

class Base {    
    int  x=10;  

    Base() {    
      show();
    }  

    void show() {   
        System.out.print (\"Base Show \         


        
4条回答
  •  故里飘歌
    2021-02-15 10:03

    Childs override of the show method is invoked because that's what the Java spec calls for. Here is a great discussion of why you should not do it. The value of x is zero because Child has not finished initializing yet.

提交回复
热议问题