Reputation: 7066
For homework, I'm working with the following three classes.
Class | Extends | Variables
--------------------------------------------------------
Person | None | firstName, lastName, streetAddress, zipCode, phone
CollegeEmployee | Person | ssn, salary,deptName
Faculty | CollegeEmployee | tenure(boolean)
I am having trouble getting the Faculty constructor to properly use the data in the superclasses.
import java.util.*;
import javax.swing.*;
public class Faculty extends CollegeEmployee
{
protected String booleanFlag;
protected boolean tenured;
public Faculty(String firstName, String lastName, String streetAddress,
String zipCode, String phoneNumber, String ssn,
String department, double salary)
{
super(firstName,lastName,streetAddress,zipCode,phoneNumber,
department,ssn,salary);
String booleanFlag = JOptionPane.showInputDialog(null, "Tenured (Y/N)?");
if(booleanFlag.equals("Y"))
tenured = true;
else
tenured = false;
}
public void setTenure(boolean tenured)
{ this.tenured = tenured; }
public boolean getTenured()
{ return tenured; }
public void display()
{
super.display();
JOptionPane.showMessageDialog(null, "Tenured: " + tenured);
}
}
The CollegeEmployee
class, from which Faculty
descends, appears below.
import java.util.*;
import javax.swing.*;
public class CollegeEmployee extends Person
{
protected String ssn;
protected String sal;
protected double annSalary;
protected String department;
public CollegeEmployee(String firstName, String lastName,
String streetAddress, String zipCode,
String phoneNumber)
{
super(firstName,lastName,streetAddress,zipCode,phoneNumber);
ssn = JOptionPane.showInputDialog(null, "Enter SSN ");
department = JOptionPane.showInputDialog(null, "Enter department: ");
sal = JOptionPane.showInputDialog(null, "Enter salary: ");
annSalary = Double.parseDouble(sal);
}
public void setFirstName(String firstName)
{ this.firstName = firstName; }
public String getFirstName()
{ return firstName; }
... ETC ... REMAINING GET/SET METHODS ELIMINATED FOR BREVITY.
The errors I'm getting point to a mismatch between parameters...Faculty
calls eight parameters, but CollegeEmployee
only has five. However, I'd think that by extending CollegeEmployee
which extends Person
, I'd have access to all eight fields by the time this class is called. As it's been pointed out, that's not the case. I only have the five fields from Person
. So my obvious next question is how I get ssn, department and salary
from CollegeEmployee
to Faculty
? That's the piece I'm missing. I've been poring over the Java Tutorials and experimenting for a couple hours, but still can't get what I need to do for the correction. Do I need to call the People
variables, via CollegeEmployee
, then instantiate the CollegeEmployee
variables in Faculty
? I'm getting really confused about what to do and desperately need some guidance...
Thanks loads, I'll be back in a few after I peruse the super() keywords section in the Tutorials.
Upvotes: 1
Views: 336
Reputation: 411
In Faculty.java you have: super(firstName,lastName,streetAddress,zipCode,phoneNumber,department,ssn,salary);
This is physically calling the constructor of CollegeEmployee which has only five parameters. This is a compilation error.
A class may have variables that are not set in the constructor. You could have empty constructors and set the variables in another method.
Since there is an inheritance structure, you may only set the parent's variables via super().
Upvotes: 2
Reputation: 121961
The super()
call in the constructor invokes the parent class constructor. In this case, the parent class CollegeEmployee
s constructor takes five arguments but is being pass eight which is incorrect.
See Keyword super Tutorial, section Subclass Constructors in particular.
Upvotes: 3