Calling an overridden method from a constructor

后端 未结 4 1182
醉话见心
醉话见心 2021-02-15 09:55

In the following example:

class Base {    
    int  x=10;  

    Base() {    
      show();
    }  

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


        
4条回答
  •  青春惊慌失措
    2021-02-15 10:08

    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.

提交回复
热议问题