Phil_oneil
Phil_oneil

Reputation: 267

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.

Upvotes: 2

Views: 1980

Answers (4)

Pshemo
Pshemo

Reputation: 124275

You need to know two things:

  1. At start of each constructor there is call to constructor of superclass. It is represented by

    • super() - invoke no-argument constructor of superclass
    • super(your,arguments) - invoke constructor handling arguments with same types as passed to super(..).

    If we want to use no-argument constructor we don't actually need to write super() because compiler will add code representing call to super() in binaries, but compiler can't do this nice thing for us if we want to call super with arguments because

    • it can't decide which constructor with arguments of superclass you want to use,
    • and what values (arguments) should be passed there,

    so in that case you need to explicitly write super(your,arguments) at start of your constructor yourself.

  2. If you will not add any constructor to your class compiler will add default constructor, which will have no arguments and its only instruction will be calling no-argument constructor of superclass super(). In other words code of classes like

    class Base{}
    class Derived extends Base{}
    

    is same as this code

    class Base extends Object{
        Base(){
            super();//will try to call Object() constructor
        }
    }
    class Derived extends Base{
        Derived(){
            super();//will try to call Base() constructor
        }
    }
    

Now since in User class there is only one constructor User(String UName,String Ufname) when you try to compile

public class Admin extends User {  

    public void manage_items() {            
        //...
    }

    public void manage_users() {   
        //...
    }
}

compiler see it as

public class Admin extends User {  

    public Admin(){
        super();
    }

    public void manage_items() {            
        //...
    }

    public void manage_users() {   
        //...
    }
}

Now as you see problem lies in

    public Admin(){
        super();
    }

because super() will try to invoke no-argument User() constructor, but there is no such constructor because only existing constructor in User class is User(String,String).

So to make this work you need to add Admin constructor and at its start call super(someString, someString).

So consider adding to your Admin class constructor like this one:

 public Admin(String aName,String Afname){
     super(aName, Afname);
     //here you can place rest of your constructors code
 }

Upvotes: 2

JamesB
JamesB

Reputation: 7894

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

Upvotes: 6

Elliott Frisch
Elliott Frisch

Reputation: 201537

Your Admin class extends User, and User doesn't have a default construtor. You need to use the constructor that takes two String(s) with super. Something like,

public Admin() {
  super("a", "b"); // <-- first line.
}

Alternatively, you could add a no-args constructor to User

public User() {
}

And then your current Admin would compile.

Upvotes: 2

Victor2748
Victor2748

Reputation: 4199

You must call the Visitor's constructor on the first like of the constructor that extends the Visitor's class, using super():

public User(String UName,String Ufname){
    super();
    setName(UName);
    setFname(Ufname);
}

and if you want Admin to have a custom constructor too, you must call:

super(uname, ufname) // String, String - pass the params of the superclass cunstructor

So:

public Admin() {
    super(someString, someString);
}

Upvotes: 3

Related Questions