28

Hello I'm new to Java, I'm getting this error in my production worker class. My Production worker constructor says explicitly invoke another constructor. I don't know what to do?.

import java.util.Date;

public class Employee
{
      private String name, number;
      private Date date;

      public Employee(String name, String number, Date date)
      {
            setName(name);
            setNumber(number);
            setDate(date);
      }

      public void setName(String n)
      {
            name = n;
      }
      public void setNumber(String n)
      {
            number = n;
            // you can check the format here for correctness
      }
      public void setDate(Date d)
      {
            date = d;
      }

      public String getName()
      {
            return name;
      }
      public String getNumber()
      {
            return number;
      }
      public Date getDate()
      {
            return date;
      }
}



public class ProductionWorker extends Employee
{
      private int shift;
      private double hourlyrate;
       // error is here (Implicit super constructor Employee() is undefined. Must explicitly invoke another constructor).
      public ProductionWorker(int shift, double hourlyrate)
      {
            setShift(shift);
            setHourlyPayRate(hourlyrate);
      }

      public void setShift(int s)
      {
            shift = s;
      }
      public void setHourlyPayRate(double rate)
      {
            hourlyrate = rate;
      }

      public int getShift()
      {
            return shift;
      }
      public double getHourlyPayRate()
      {
            return hourlyrate;
      }
}
Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
user659658
  • 285
  • 1
  • 4
  • 8

6 Answers6

31

Any constructor for any class as you know creates an object. So, the constructor should contain proper initialization code for its class. But if you have some class which extends another one (lets call it "parent") then constructor for the class cannot contain all the code needed for the initialization by definition (for example, you cannot define private fields of the parent). That's why constructor of the class has to call constructor of its parent. If you do not call it explicitly then the default parent constructor is called (which is without any parameter).

So, in your case, you can either implement default constructor in parent or directly call any constructor in the class.

xappymah
  • 1,624
  • 10
  • 14
  • Default constructor like this public Employee(String hourlyrate, String shift) { shift = ""; hourlyrate = ""; } – user659658 Mar 30 '11 at 15:17
  • No, a default constructor means a constructor without parameters, such as public Employee(){} – Erkan Haspulat Mar 30 '11 at 15:18
  • I did this and still get the same error. – user659658 Mar 30 '11 at 15:19
  • No, "default constructor" is a constructor which doesn't take any parameter: `public Employee() { /* Code here */ }` – xappymah Mar 30 '11 at 15:19
  • 4
    Constructors don't create objects, they only initialize them. – Joachim Sauer Mar 30 '11 at 15:40
  • @Joachim, well, as a JVM developer (one of its implementations) I do know what constructors do. I used the word "creates" just for simplification (also all my explanation is simplificated too) because OP as you see is "new to Java". EDIT: sorry if my comment looks rude - it is not intentionally. – xappymah Mar 30 '11 at 15:50
  • @xappymah No, the default constructor is the _implicit_ parameterless, bodyless constructor Java creates for us. If you create a parameterless constructor, it won't be the "default" one, it just supersedes it. – PhiLho Dec 13 '13 at 06:07
9

As others have already mentioned you are required to provide a default constructor public Employee(){} in your Employee class.

What happens is that the compiler automatically provides a no-argument, default constructor for any class without constructors. If your class has no explicit superclass, then it has an implicit superclass of Object, which does have a no-argument constructor. In this case you are declaring a constructor in your class Employee therefore you must provide also the no-argument constructor.

Having said that Employee class should look like this:

Your class Employee

import java.util.Date;

public class Employee
{
      private String name, number;
      private Date date;

      public Employee(){} // No-argument Constructor

      public Employee(String name, String number, Date date)
      {
            setName(name);
            setNumber(number);
            setDate(date);
      }

      public void setName(String n)
      {
            name = n;
      }
      public void setNumber(String n)
      {
            number = n;
            // you can check the format here for correctness
      }
      public void setDate(Date d)
      {
            date = d;
      }

      public String getName()
      {
            return name;
      }
      public String getNumber()
      {
            return number;
      }
      public Date getDate()
      {
            return date;
      }
}

Here is the Java Oracle tutorial - Providing Constructors for Your Classes chapter. Go through it and you will have a clearer idea of what is going on.

Bartzilla
  • 2,768
  • 4
  • 28
  • 37
3

ProductionWorker extends Employee, thus it is said that it has all the capabilities of an Employee. In order to accomplish that, Java automatically puts a super(); call in each constructor's first line, you can put it manually but usually it is not necessary. In your case, it is necessary because the call to super(); cannot be placed automatically due to the fact that Employee's constructor has parameters.

You either need to define a default constructor in your Employee class, or call super('Erkan', 21, new Date()); in the first line of the constructor in ProductionWorker.

Erkan Haspulat
  • 12,032
  • 6
  • 39
  • 45
  • It has nothing to do with the parameter types. An explicit call to a parent class constructor is required because the parent doesn't have a no-arg constructor. – Isaac Truett Mar 30 '11 at 15:20
  • others answered wrong, you don't have to have any default constructors like `public Employee(){};` you only need to call constructors of your super class. inside the constructor of its children. like this: `public ProductionWorker(int shift, double hourlyrate) { super(name, number, date); setShift(shift); setHourlyPayRate(hourlyrate); }` – M D P Aug 01 '16 at 05:17
2

An explicit call to a parent class constructor is required any time the parent class lacks a no-argument constructor. You can either add a no-argument constructor to the parent class or explicitly call the parent class constructor in your child class.

Isaac Truett
  • 8,734
  • 1
  • 29
  • 48
1

This problem can also come up when you don't have your constructor immediately call super.

So this will work:

  public Employee(String name, String number, Date date)
  {
    super(....)
  }

But this won't:

  public Employee(String name, String number, Date date)
  {
    // an example of *any* code running before you call super.
    if (number < 5)
    {
       number++;
    }

    super(....)
  }

The reason the 2nd example fails is because java is trying to implicitely call

super(name,number,date)

as the first line in your constructor.... So java doesn't see that you've got a call to super going on later in the constructor. It essentially tries to do this:

  public Employee(String name, String number, Date date)
  {
    super(name, number, date);

    if (number < 5)
    {
       number++;
    }

    super(....)
  }

So the solution is pretty easy... Just don't put code before your super call ;-) If you need to initialize something before the call to super, do it in another constructor, and then call the old constructor... Like in this example pulled from this StackOverflow post:

public class Foo
{
    private int x;

    public Foo()
    {
        this(1);
    }

    public Foo(int x)
    {
        this.x = x;
    }
}
Community
  • 1
  • 1
Brad Parks
  • 66,836
  • 64
  • 257
  • 336
0

Had this problem recently in my comp lab. It's simple and Erkan answered it correctly. Just put super("the name of your subclass") So in relation to your problem --> super("ProductionWorker); as the first line of your subclass' constructor.

Lyuben Todorov
  • 13,987
  • 5
  • 50
  • 69