Java inheritance and super() isn't working as

2019-07-20 11:43发布

问题:

For a homework assignment, I'm working with the following. It's an assigned class structure, I know it's not the best design by a long shot.

Class           | Extends         | Variables
--------------------------------------------------------
Person          | None            | firstName, lastName, streetAddress, zipCode, phone
CollegeEmployee | Person          | ssn, salary,deptName
Faculty         | CollegeEmployee | tenure(boolean)
Student         | person          | GPA,major

So in the Faculty class...

 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,
                 ssn,department,salary);
           String booleanFlag = JOptionPane.showInputDialog
                                (null, "Tenured (Y/N)?");
           if(booleanFlag.equals("Y"))
           tenured = true;
           else
           tenured = false;
       }
 }

It was my understanding that super() in Faculty would allow access to the variables in CollegeEmployee as well as Person. With the code above, it compiles fine when I ONLY include the Person variables. As soon as I try to use ssn, department, or salary I get the following compile errors.

 Faculty.java:15: error: constructor CollegeEmployee in class CollegeEmployee can not be applied to the given types:
            super(firstName,lastName,streetAddress,zipCode,phoneNumber,ssn,department,salary);
                                                                                            ^
Required: String,String,String,String,String
   Found: String,String,String,String,String,String,String,String
  reason: actual and formal argument lists differ in length

I'm completely confused by this error...which is the actual and formal? Person has five arguments, CollegeEmployee has 3, so my guess is that something's funky with how the parameters are being passed...but I'm not quite sure where to begin fixing it. What am I missing?

Here's my CollegeEmployee class

 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 setSSN(String ssn)
     {         this.ssn = ssn;         }
     public void setAnnSalary(double annSalary)
     {        this.annSalary = annSalary;     }
     public void setDepartment(String department)
     {        this.department = department;    }
 //
     public String getSSN()
     {        return ssn;                }
     public double getAnnSalary()
     {        return annSalary;          }
     public String getDepartment()
     {        return department;         }
 //
     public void display()
     {
         super.display();
         JOptionPane.showMessageDialog
         (null, "\nSSN: " + getSSN() + "\nAnnual Salary: " 
          + getAnnSalary() + "\nDepartment: " + getDepartment(), 
          "Information", JOptionPane.QUESTION_MESSAGE);
     }
 }

回答1:

You made an accidental ; at the end of your constructor:

      public Faculty(String firstName, String lastName, String streetAddress,   String zipCode, String phoneNumber,String ssn,
           String Department,double salary); // <--- this has to go

And as pointed out in a comment you have String Department in your constructor, but you pass department (incorrect capitalization), rename your constructor argument to String department



回答2:

Remove the ; at the end of the constructor declaration line:

public Faculty(String firstName, String lastName, String streetAddress, String zipCode, String phoneNumber,String ssn,String Department,double salary);
//----------------------------------------------------------------------------------------------------------------------------------------------------^


回答3:

Remove the ';' at the end of 'ment,double salary);'

public Faculty(String firstName, String lastName, String streetAddress, String zipCode, String phoneNumber,String ssn,String Department,double salary); <--- remove this
      {


回答4:

Your super class CollegeEmployee needs to have a constructor with this eight arguments, also the argument types should match

public class CollegeEmployee
{
    public CollegeEmployee(String firstName, String lastName, String streetAddress, String zipCode,
                        String phoneNumber, String ssn, String department, double salary)
    { 
        // do stuff
    }
}