简体   繁体   中英

Call Superclass method from overridden Subclass method

I'm sure this has a simple solution, but I'm new to Java and can't work it out.

I have a subclass Payroll that extends a superclass Pay, it contains an overridden method called 'calc_payroll'. From this method, I want to call the superclass method of the same name, and assign the output to a variable in the overriding method. My code is below

public class Payroll extends Pay
{
   public double calc_Payroll()
{
    double grossPay = super.calc_Payroll();
    double taxAmt = tax(grossPay);
    double netPay = grossPay - taxAmt;  

    System.out.println(grossPay);

    return netPay;
}


}

Below is the code from the calc_payroll method in the superclass

public double calc_Payroll()
{
    double otRate = rate * 1.77;
    double otHours = ttlHours - stHours;

    if(stHours == 0)
    {
        grossPay = otHours * rate;
    }

    else
    {
        grossPay = ((stHours * rate) + (otHours * otRate));
    }

    System.out.println(stHours + "//" + otHours + "//" + rate);//for testing

    return grossPay;
}

the superclass method functions without issue to calculate and return the gross pay when called from a different subclass, but when calling it from a method with the same name, the print line in the code above (that I have labelled for testing) displays zero's for all variables

Code for full 'Pay' class is below as requested

public class Pay
{
private double ttlHours;
private int stHours;
private double rate;
double grossPay = 0.0;
final double TAXL = 0.07;
final double TAXM = 0.1;
final double TAXH = 0.16;

public void SetHours(double a)
{
    ttlHours = a;
}

public void SetHoursStr(int a)
{
    stHours = a;
}

public void SetRate(double a)
{
    rate = a;
}

public double GetHours()
{
    return ttlHours;
}

public int GetStHours()
{
    return stHours;
}

public double GetRate()
{
    return rate;
}

public double taxRate()
{
    double taxRate = 0.0;

    if(grossPay <= 399.99)
    {
        taxRate = TAXL;
    }
    else if(grossPay <= 899.99)
    {
        taxRate = TAXM;         
    }
    else
    {
        taxRate = TAXH;
    }

    return taxRate;
}

public double tax(double grossPay)
{
    double ttlTax = 0.0;        

    if(grossPay < 400.00)
    {
        ttlTax += (grossPay * TAXL); 
    }

    else if(grossPay < 900.00)
    {
        ttlTax += (grossPay * TAXM);
    }

    else
    {
        ttlTax += (grossPay * TAXH);
    }

    return ttlTax;
}

public double calc_Payroll()
{
    double otRate = rate * 1.77;
    double otHours = ttlHours - stHours;

    if(stHours == 0)
    {
        grossPay = otHours * rate;
    }

    else
    {
        grossPay = ((stHours * rate) + (otHours * otRate));
    }

    System.out.println(stHours + "//" + otHours + "//" + rate);//for testing

    return grossPay;
}
}

The subclass Payroll contains no other code

Below is the code that accepts user input to assign values to the initialized variables

public class CalPayroll extends Pay
{
Payroll nPay = new Payroll();
Accept Read = new Accept(); 


public void AcceptPay()
{
    char select = '0';

    while(select != 'e' && select != 'E')
        {
            System.out.println("Payroll Computation \n");           
            System.out.print("Enter number of hours worked (00.0) <0 for Quick exit>: ");
            SetHours(Read.AcceptInputDouble());
            System.out.print("Enter first number of hours straight (integer or 0 to disable): ");
            SetHoursStr(Read.AcceptInputInt());
            System.out.print("Enter hourly rate of worker (00.00): ");
            SetRate(Read.AcceptInputDouble());
            Screen.ScrollScreen('=', 66, 1);
            Screen.ScrollScreen(1); 
            displayInfo();
            System.out.println("e to exit, any other letter + <Enter> to continue");
            select = Read.AcceptInputChar();
        }
}

public void displayInfo()
{       
    NumberFormat currency = NumberFormat.getCurrencyInstance();
    NumberFormat percent = NumberFormat.getPercentInstance();

    System.out.println("Gross pay is :" + currency.format(calc_Payroll()));
    System.out.println("Tax is :" + percent.format(taxRate()));
    System.out.println("Net pay is :" + currency.format(nPay.calc_Payroll()));      
    Screen.ScrollScreen(1);     
}


}

I'm confused!

Its clear from you code that ttlHours, stHours and rate are not initialised with some reasonable value. So when you just call super.calc_Payroll() , values like 0 or 0.0 are used as i explained in my comment. Its good to first set values of these variables before calling super.calc_Payroll() .

SetHours(23.4);  //some value

SetHoursStr(5);   //some value

SetRate(2.3);   //some value

Also you don't have constructor for Pay class, try making it and initialising all uninitialised variable in constructor or use setter/getter methods to set and get values.

Since your both classes extends Pay class, it creates the problem which you are facing. When you call SetHours(Read.AcceptInputDouble()) , it set the variable inherited by CalPayroll from Pay , not the variables inherited by Payroll class. What you have to do is to set variables for Payroll instance as well as for current class as both extends Pay . Do the following replace your while loop as,

while(select != 'e' && select != 'E')
        {
            System.out.println("Payroll Computation \n");           
            System.out.print("Enter number of hours worked (00.0) <0 for Quick exit>: ");
            SetHours(Read.AcceptInputDouble());
            nPay.SetHours(GetHours());
            System.out.print("Enter first number of hours straight (integer or 0 to disable): ");
            SetHoursStr(Read.AcceptInputInt());
            nPay.SetHoursStr(GetStHours());
            System.out.print("Enter hourly rate of worker (00.00): ");
            SetRate(Read.AcceptInputDouble());
            nPay.SetRate(GetRate());
            Screen.ScrollScreen('=', 66, 1);
            Screen.ScrollScreen(1); 
            displayInfo();
            System.out.println("e to exit, any other letter + <Enter> to continue");
            select = Read.AcceptInputChar();
        }

Please post the complete code. It seems that for some reason your variables of super class method not getting assigned values properly. And they are initialized with their default values which is making everything 0. I'll be able to help better if you paste the complete class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM