简体   繁体   中英

Implicit super class constructor MyNumber() is undefined. Must invoke another constructor

I am getting the error mentioned in title in the following code. Please tell me why am I getting this error, although i haven't called a default constructor MyNumber() from the superclass any where, and how to fix it.

package referencereturntype;

public class MyNumber { String num;

public MyNumber(String str){
    num=str;
}

public static void main (String [] args){
    MyNumber my_num= new MyNumber("+2");
    System.out.println("Success! The object of the class itself is successfully returned from retOb(). The object now contains the string : " + retOb(my_num).num);
}

public static SubMyNumber retOb(MyNumber my_num){
    SubMyNumber sub_my_num= new SubMyNumber("-50");
    sub_my_num.nums=my_num.num;
    return sub_my_num;
}

}

public class SubMyNumber extends MyNumber { String nums;

public SubMyNumber( String strs){
    nums=strs;
}   

}

Thanks in advance.

As you don't have default constructor you have to call it explicit.

public class SubMyNumber extends MyNumber { 
    String nums;
    public SubMyNumber( String strs){
        super(null); // Or the value you want to superClass
        this.nums=strs;
    }

You must call a constructor from the base class when instantiating the derived class.
If you don't, Java will implicitly call the default constructor.

If there is no default constructor, you get that error.

From JLS 8.8.7

If a constructor body does not begin with an explicit constructor invocation and the constructor being declared is not part of the primordial class Object, then the constructor body implicitly begins with a superclass constructor invocation "super(); ",

Since there is no no-args constructor in the super-class the constructor with the String argument must be explicitly invoked

当超类没有默认的空构造函数时,则必须从子类的构造函数中显式调用super

The thing is if there is no constructor defined in your class, java internally provides a default constructor which is further used to create objects. In case you provide you own constructor (can be either default or parameterized), the Java provided hidden constructor gets lost. In your case, you have provided a parameterized constructor in 'MyNumber'. Thus the hidden default constructor is no more available to be called from the child class's constructor (super()).

The only change you need to do in this code is to either 1. Give your own default constructor in addition to the parameterized constructor which is already there. Now, when the default constructor of class 'MyNumber' shall be INTERNALLY called from the first LOC in the parameterized constructor of 'SubMyNumber', there won't be any error.

  1. Or you can change the parameterized constructor of subn class like this

    public SubMyNumber(String strs){ super(strs); nums=strs; }

This way you shall explicitly set the parameterized constructor of MyNumber to be called which is already defined.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM