problems with sub-class constructor
I made a class that generates an Employee, and then started on a Worker sub-class extending the Employee. For the worker, I have a constructor that checks back to the Employee-super class, which works fine, but after I've typed up a new contructor using parameters unique to the Worker sub-class, it highlights the first { of this second constructor and reads "Error: No match was found for constructor 'Employee ()'. I don't want this constructor to check back to the super class; i've tried making a new constructor in another class that uses one parameter from the super class and one parameter unique to the subclass and it works fine, but this one won't work. Is it because I'm using both my parameters from sub-class?
Here's some code:
// variables unique to the sub-class Worker
int hoursWorked;
double wage;
// this first constructor works perfectly
public Worker (String workerName, String deprt)
{
super (workerName, deprt);
this.hoursWorked = (int) (Math.random () * 35) + 5;
this.wage = (Math.random () * 1) + 8.00;
this.securityLvl = 0;
this.idCode = (int) (Math.random () * 9000) + 725000;
}
// ... this one does not.
public Worker (int hoursWorked, double wage)
{ // this bracket is highlighted with the error message
this.hoursWorked = hoursWorked;
this.wage = wage;
String randNames [] = {"George", "Jane", "Francis", "Margaret", "Robert", "Alice"};
int randNo = (int) (Math.random () * 6);
this.name = randNames [randNo];
this.deprt = "Main Area";
this.securityLvl = 0;
this.idCode = (int) (Math.random () * 9000) + 725000;
}
Just a heads up, I'm using the Ready to Program IDE, in case that might help narrow down a list of possible corrections.
Let's say we have two classes, MyBaseClass and MyExtendedClass which extends MyBaseClass. Whether or not you specify a constructor for MyExtendedClass, you must make sure you always use a constructor from MyBaseClass.
If MyBaseClass specifies a default constructor (a parameter-less one that is), then nothing special has to be done. But otherwise, you need to specifically call a constructor using the "super" keyword. Here's an example:
public class MyBaseClass {
public MyBaseClass(String parameter) {
//some initialization code here
}
}
public class MyExtendedClass extends MyBaseClass {
public MyExtendedClass() {
super("We call a specific constructor this way");
}
public MyExtendedClass(String anotherWay) {
super(anotherWay);
}
}
Yes, I knew about that so far. The reason why I'm confused is that I have another pair of class and sub-class that has an alternate constructer that doesn't call the super class and has a parameter unique to the sub-class.
For example, I have MyBaseClass2 and MyExtendedClass2...
// this variable is unique to the sub-class and not found in the super
int variableFromExtended
// the first constructor using parameters calling the super class
public Extended (int variableFromBase, int variableFromBase2)
{
super (variableFromBase, variableFromBase2)
// variables from parameters given set here
// other variables set randomly or otherwise
}
//the second constructor uses one variable from the super class and the
//unique variable
public Extended (int variableFromBase3, int variableFromExtended)
{
//all variables set here either randomly or from given parameters
}
Message was edited by:
-cobra.code-
I just found out the problem.
In the super class, I needed a constructor that set all parameters randomly or otherwise. The alternate constructor doesn't work because it doesn't match any constructors in the super class, but once I added a constructor where no parameters are given like
public Employee () {}
it acts as a wildcard for any constructors in the extended class.
Thanks for trying to help man, you were the only person who replied.