Programming assignment using inheritance
I am writing a java program focusing on inheritance. The super class reads in a file and I tokenize the line and send each token into its own array. There are 7 arrays with 9 slots in each one (because there are 9 lines of information and 6 tokens per line). I have to make a sub class extending from the super class, and I am having problems with my super class constructor and how the sub class will be able to access the information in any of the slots of any of the arrays. I understand the basic concept of inheritance, but am having trouble comprehending how to put arrays as parameters in the constructor and how the subclass will access those values.
Code Java:
//Super Class constructor
public Employee(String employFirstNames[], String employLastNames[], String employNumbers[], String employHireDates[], String employShiftNumbers[], String employHourlyRates[])
{
for(i = 0; i < employees.length(); i++)
{
empFirstNames[] = employFirstNames[];
empLastName[] = employLastNames[];
empNumbers[] = employNumbers[];
empHireDates[] = employHireDates[];
empShiftNumbers[] = employShiftNumbers[];
empHourlyRates[] = employHourlyRates[];
}
}
Code Java:
//sub class constructor
public ProWorker()
{
super(empFirstNames[], empLastNames[], empNumbers[], empHireDates[], empShiftNumbers[], empHourlyRates[]);
}
Re: Programming assignment using inheritance
The subclass constructor does not have any parameters, so based upon the info you provided cannot invoke the super class constructor. You can adapt the child class constructor to accept parameters and pass those to the parent class, or provide setters for those values which can be used to set the values, or access them directly if the child class has access. The first suggestion is demonstrated below:
Code java:
public class Parent{
private String[] values;
public Parent(String[] values ){
this.values = values;
}
}
public class Child extends Parent{
public Child(String[] values){
super(values);
}
}