Undefined constructor (java)

后端 未结 4 953
遇见更好的自我
遇见更好的自我 2021-01-21 23:16

so I have a java class called User that contains a constructor like this:

public class User extends Visitor {
    public User(String UName, String Ufname){
              


        
4条回答
  •  日久生厌
    2021-01-21 23:23

    If you don't add a constructor to a class, one is automatically added for you. For the Admin class, it will look like this:

    public Admin() { 
        super();
    }
    

    The call super() is calling the following constructor in the parent User class:

    public User() {
        super();
    }
    

    As this constructor does not exist, you are receiving the error message.

    See here for more info on default constructors: http://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html

    You don't have to provide any constructors for your class, but you must be careful when doing this. The compiler automatically provides a no-argument, default constructor for any class without constructors. This default constructor will call the no-argument constructor of the superclass. In this situation, the compiler will complain if the superclass doesn't have a no-argument constructor so you must verify that it does. If your class has no explicit superclass, then it has an implicit superclass of Object, which does have a no-argument constructor.*

提交回复
热议问题