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;
}
}
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.