Search code examples
javaoopinheritancesuperabstraction

some params not being passed to super() construtor


I have a problem passing params into a super constructor. I'm implementing Abstraction with Inheritance. I created three classes that extends the abstract class Employee. But when I created the objects of these three classes, only one class passes the arguments correctly to the super() constructor, while the rest will return null. Here is the abstract class

public abstract class  Employee {

    private String firstName,lastName,SSN;


    public Employee(String fName, String lName, String SSN){
        this.SSN = SSN;
        this.firstName = fName;
        this.lastName = lName;

    }
    public void setFirstName(String fName){
        this.firstName = firstName;
    }
    public void setLastName(String lName){
        this.lastName = lName;
    }
    public void setSSN(String SSN){
        this.SSN = SSN;
    }
    public String getSSN(){
        return SSN;
    }


    public String getFirstName(){
       return firstName;
    }
    public String getLastName(){
       return lastName;
    }
    public abstract double getEarnings();

    @Override
    public  String toString(){
       return String.format("First Name: %-2s \n Last Name: %-2s \n Social Security Number: %-2s", getFirstName(), getLastName(),getSSN());
    }

And here are the classes:

public class SalariedEmployee extends Employee {

    private String firstName, lastName, SSN;
    double weeklyPay;

    public SalariedEmployee(String fName, String lName, String SSN, double wPay) {
        super(fName, lName, SSN);
        this.weeklyPay = wPay;
    }

    public void setWeeklyPay(double wPay) {
        this.weeklyPay = wPay;
    }

    public double getWeeklyPay() {
        return weeklyPay;
    }

    @Override
    public double getEarnings() {
        return getWeeklyPay();
    }

    @Override
    public String toString() {
        return String.format("%s \n Weekly Pay: %2s", super.toString(), getEarnings());
    }

}

public class HourlyEmployee extends Employee{
    private String firstName, lastName, SSN;
    private double hourlyPay,wage;
    private int hours;

    public HourlyEmployee(String fName, String lName, String SSN,  int hour, double hourlyPay ){
        super(fName, lName, SSN);
        this.hours = hour;
        this.hourlyPay = hourlyPay;

    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getSSN() {
        return SSN;
    }

    public void setSSN(String SSN) {
        this.SSN = SSN;
    }

    public double getHourlyPay() {
        return hourlyPay;
    }

    public void setHourlyPay(double hourlyPay) {
        this.hourlyPay = hourlyPay;
    }

    public double getWage() {
        return wage;
    }

    public void setWage(double wage) {
        this.wage = wage;
    }

    public int getHours() {
        return hours;
    }

    public void setHours(int hours) {
        this.hours = hours;
    }

    @Override
    public double getEarnings(){
        double earnings;
        if(hours> 40)
            earnings = hourlyPay * 40 + ((hours-40) *hourlyPay *1.5);
        else
        earnings  = hourlyPay * hours;

       return earnings;
    }
    @Override
    public String toString(){
        return String.format("%s \n Hours worked: %s \n Salary: %s \n ", super.toString(), getHours(), getEarnings());
    }

}

public class CommissionEmployee extends Employee {
    private String firstName, lastName, SSN;
    private double sales,commission;
    public CommissionEmployee(String fName, String lName, String SSN, double sales, double commission){
       super(fName, lName, SSN);
       this.commission = commission;
       this.sales = sales;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getSSN() {
        return SSN;
    }

    public void setSSN(String SSN) {
        this.SSN = SSN;
    }

    public double getSales() {
        return sales;
    }

    public void setSales(double sales) {
        this.sales = sales;
    }

    public double getCommission() {
        double commissionRate;
        if(commission >0 && commission<1)
        return commission;
        else
            return 0;
    }

    public void setCommission(double commission) {
        this.commission = commission;
    }
    @Override
    public double getEarnings(){
        return getCommission() * getSales();
    }
    @Override
    public String toString(){
        return String.format("%s \n Commission Rate: %s \n Salary: %s \n", super.toString(), getCommission(), getEarnings());
    }
}

And Here is the TEST Class

public class EmployeeTest {

    public static void main(String[] args) {
        Employee salariedEmp,hourlyEmp,commissionEmp;
         salariedEmp = new SalariedEmployee("Aminu", "Bishir", "11-22-33-33", 300.47);
        System.out.println(printDetails(salariedEmp));
         hourlyEmp = new HourlyEmployee("Musa","Sale","22-33-44-55",50,20.4);
        System.out.println(printDetails(hourlyEmp));
         commissionEmp = new CommissionEmployee("Muhammad","Ibrahim","33-44-55-66",20000,0.3);
       System.out.print( printDetails(commissionEmp));
    }
    public static String printDetails(Employee emp){
        return String.format("%s \n %s",emp.getClass(),emp.toString());
    }

}

And this is the output I get whenever I run it:

class Abstraction.SalariedEmployee   
 First Name: Aminu   
 Last Name: Bishir   
 Social Security Number: 11-22-33-33   
 Weekly Pay: 300.47  

class Abstraction.HourlyEmployee    
 First Name: null     
 Last Name: null   
 Social Security Number: null   
 Hours worked: 50  
 Salary: 1122.0   


class Abstraction.CommissionEmployee 
 First Name: null   
 Last Name: null   
 Social Security Number: null   
 Commission Rate: 0.3   
 Salary: 6000.0  

Solution

  • You have a problem with declaring variables multiple times. By declaring them in a super class, they also get declared in a subclass. When you declare the variables with the same name in a subclass, you can address the superclass' variable via super.variable. But I would recommend you to simply delete the variable declarations in your subclasses, that were already declared in the superclass.

    However this is not the root of your problem. A similar issue occures if you override methods, in your case the subclass that prints correctly does not override the getters, which are used to read out the information stored in your variables. In the class that prints out correctly the getters are found in the superclass and hence the getters are addressing the superclass variables. In the other 2 classes the overridden getters are found, that are using the variables found in the subclasses. Since you set only the variables in the superclass, the subclass variables are always null and so is your result.

    I would recommend you to delete the overriden getters and the variables in the subclasses. You don't need them.