Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undefined constructor (java)

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){
        setName(UName);
        setFname(Ufname);
    }
}

And then the he problems occur in my other class called Admin:

public class Admin extends User {  //error "Implicit super constructor User() is undefined for default constructor. Must define an explicit constructor"

//public Admin() { } //tried to add empty constructor but error stays there

    //}


    public void manage_items() {            

        throw new UnsupportedOperationException();
    }

    public void manage_users() {   

        throw new UnsupportedOperationException();
    }
}

I am getting the error Implicit super constructor User() is undefined for default constructor. Must define an explicit constructor at the place marked in the code. I don't know how to fix that, and I'm really new to java.

like image 718
Phil_oneil Avatar asked Mar 15 '26 03:03

Phil_oneil


1 Answers

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.*

like image 121
JamesB Avatar answered Mar 16 '26 17:03

JamesB



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!