Search code examples
javainheritancesubclassinvokesuperclass

Using returns from two different methods in subclass


I'm struggling with one of the final parts of a school assignment.

I had asked this question on another question I had but did not receive an answer. I have two methods in my super class that I need to use in my sub class and the one method must be invoked inside of the other to return a result, I'm stumped on how to do this.

public class Pay
{
private float hours;
private float rate;
private int hrsStr;
float gross;
double tax; 

public void calc_Payroll()
{
    if (hrsStr != 0)
        gross = hrsStr + ((hours - hrsStr) * 1.33f) * rate;
    else
        gross = hours * rate;
}

public void tax(double a)
    {
    if (gross <= 399.99)
                tax = .92;
                else
                    if (gross <= 899.99)
                        tax = .88;
                    else 
                        tax = .84;
    }


    public void setHours(float a)
    {
        hours = a;
    }

    public float getHours()
    {
        return hours;
    }

    public void setRate(float a)
    {
        rate = a;
    }

    public float getRate()
    {
        return rate;
    }

    public void setHrsStr(int a)
    {
        hrsStr = a;
    }

    public int getHrsStr()
    {
        return hrsStr;
    }
}

That is the entire superclass and i need to call the calc_Payroll() method and the tax() method to the subclass, well I need tax() to be inside calc_Payroll() because I need to calculate the net pay from those two methods.

public class Payroll extends Pay
{
float net;
@Override
public void calc_Payroll()
    {
                 //I need to calculate the the net pay here. 
    }
}

Solution

  • Are you struggling with the syntax of Java?

    public class Payroll extends Pay
    {
        float net;
    
        @Override
        public void calc_Payroll()
        {
            // you can call `super.calc_Payroll()`
            // you can call `tax(double a)`
        }
    }