简体   繁体   中英

“no default constructor available in abstract class”

I'm getting a no default constructor available error in only one of my concrete classes that implement the same abstract class and I'm not sure why, any help is much appreciated.

`

public abstract class Employee implements Payable
    private  String firstName;
    private  String lastName;
    private  String socialSecurityNumber;
    private date birthdate;

          // constructor
          public Employee(String firstName, String lastName,
             String social, date dob )
          {
             this.firstName = firstName;
             this.lastName = lastName;
            this.socialSecurityNumber = social;
             //this.birthdate = getBirthdate();
              // Birthdate(year, month, day);
              birthdate = dob;
          }



public class pieceWorker extends Employee  // no default constructor available
{
    private double wage; `` 
    private int pieces;


    public void pieceWorker(String firstName, String lastName, String social,date dob, double wage, int pieces )  // use some getters ?
    {
        super(firstName,lastName,social, dob );
        setWage(wage);
        setPieces(pieces);
        this.wage = wage;
        this.pieces = pieces;
    }

You've specified a return type of void for your constructor. Since constructors can't have return types, you're getting this error because you haven't actually defined a constructor, it's just a regular method. You'll need to remove void from your method in pieceWorker to make it a constructor, like this:

public pieceWorker(String firstName, String lastName, String social,Date dob, double wage, int pieces )
    {
        super(firstName,lastName,social, dob );
        setWage(wage);
        setPieces(pieces);
        this.wage = wage;
        this.pieces = pieces;
    }

Also, unless date is a class you've created, you may want to change it to Date , in the java.util package.

As there is no default (or no-arg) constructor in the parent abstract class the constructor used in sub classes must be specified.

In sub class you do not have a constructor specified as you have add the return type void. Please modify the code as below.

public abstract class Employee implements Payable
    private  String firstName;
    private  String lastName;
    private  String socialSecurityNumber;
    private date birthdate;

          // constructor
          public Employee(String firstName, String lastName,
             String social, date dob )
          {
             this.firstName = firstName;
             this.lastName = lastName;
            this.socialSecurityNumber = social;
             //this.birthdate = getBirthdate();
              // Birthdate(year, month, day);
              birthdate = dob;
          }



public class pieceWorker extends Employee  // no default constructor available
{
    private double wage; 
    private int pieces;


    public pieceWorker(String firstName, String lastName, String social,date dob, double wage, int pieces )  // use some getters ?
    {
        super(firstName,lastName,social, dob );
        setWage(wage);
        setPieces(pieces);
        this.wage = wage;
        this.pieces = pieces;
    }
}

As Mentioned by @Gulllie and @dammina (almost at the same time), class pieceWorker is having a method

public void pieceWorker(args...) 

and not a constructor. Hence, class pieceWorker will use a default constructor. The first line of any constructor is call to super(). if you do not explicitly call super(), java will do that for you.

public abstract class Employee implements Payable{
private  String firstName;
private  String lastName;
private  String socialSecurityNumber;
private date birthdate;

      // constructor
      public Employee(String firstName, String lastName,
         String social, date dob )
      {
         this.firstName = firstName;
         this.lastName = lastName;
        this.socialSecurityNumber = social;
         //this.birthdate = getBirthdate();
          // Birthdate(year, month, day);
          birthdate = dob;
      }
}

you don't have default constructor in above abstract class, as you have defined your own constructor. Compiler will do following modification to your pieceWorker class.

public class pieceWorker extends Employee  // no default constructor available
{
private double wage; 
private int pieces;

// compiler will insert following constructor
public pieceWorker(){
    super();
}

public void pieceWorker(String firstName, String lastName, String social,date dob, double wage, int pieces )  // use some getters ?
{
    super(firstName,lastName,social, dob );
    setWage(wage);
    setPieces(pieces);
    this.wage = wage;
    this.pieces = pieces;
}
}

You can do following to make your code work:

public class pieceWorker extends Employee  // no default constructor available
{
private double wage; 
private int pieces;

public pieceWorker(String firstName, String lastName, String social,date dob, double wage, int pieces )  // use some getters ?
{
    super(firstName,lastName,social, dob );
    setWage(wage);
    setPieces(pieces);
    this.wage = wage;
    this.pieces = pieces;
}
}

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