Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java. Implicit super constructor Employee() is undefined. Must explicitly invoke another constructor [duplicate]

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;       } } 
like image 327
user659658 Avatar asked Mar 30 '11 15:03

user659658


People also ask

How do you fix implicit super constructor is undefined?

There are two ways to resolve this error. Define an explicit default constructor in parent class. As stated before, if a class contains a constructor, the compiler does not automatically generate default constructor. Explicitly invoke parent class constructor from within the child constructor using super() .

What is an implicit super constructor?

implicit super constructor is undefined for default constructor. must define an explicit constructor” this compilation error is caused because the super constructor is undefined. in java, if a class does not define a constructor, compiler will insert a default one for the class, which is argument-less.

What is implicit super constructor in Java?

Subclass constructors must invoke the superclass constructor, either explicitly or implicitly. Either way, a super constructor must be defined. A class that has no parent has the Object class as its parent. The Object class in Java has a constructor with no arguments.


2 Answers

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.

like image 168
xappymah Avatar answered Sep 20 '22 04:09

xappymah


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.

like image 34
Bartzilla Avatar answered Sep 22 '22 04:09

Bartzilla