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){
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.*
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);
}
You need to know two things:
At start of each constructor there is call to constructor of superclass. It is represented by
super()
- invoke no-argument constructor of superclasssuper(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
so in that case you need to explicitly write super(your,arguments)
at start of your constructor yourself.
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
}
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.