lil poggers
lil poggers

Reputation: 13

why i am getting an error when i try to over ride a method

Hello and good morning guys I'm new to Java, I've encountered some problems while doing my home work. It is related to inheritance.

I need to create a super class named Employee where the variable name and Employee number are kept. And then I need to create a displayInfo() methods to print all the variables.

Then I've been assigned to create a subclass where i calculate the salary and make an overridden method to print the variables from the super class and add the salary to it using the displayInfo() method. But when I try to use it in the subclass, there is an error shown up where it says:

the field employee.number is not visible

Is there any way I can solve this?

Thank you very much for your help! Here is the code I've tried so far

public class Employee {
    private String name;
    private String number;
    private String date;
    
    public Employee()
    {
        
    }
    
    public Employee(String name,String number,String date)
    {
        this.name=name;
        this.number=number;
        this.date=date;
    }
    
    public String getName()
    {
        return name;
    }
    public String getNumber()
    {
        return number;
    }
    public String getDate()
    {
        return date;
    }
    
    public void displayInfo()
    {
        System.out.println("Name = "+ name + "Number = "+number+ " Date = " + date);
                    
    }

}
\\ my sub class
public class ProductionWorker extends Employee {

    private int shift;// 1 = day, 2 = night 
    private double HoursOfWorking;// day = RM10, night = RM12.50
    
    public ProductionWorker(int shift,int HoursOfWorking)
    {
        this.shift=shift;
        this.HoursOfWorking=HoursOfWorking;
    }
    
    public int getShift()
    {
        return shift;
    }
    public double getHoursOfWorking()
    {
        return HoursOfWorking;
    }
    
    public double calculateSalary()
    {
        if(shift==1)
        {
            return HoursOfWorking * 10 * 20;
        }
        else
        {
            return HoursOfWorking * 12.5 * 20;
        }
        
    }
    

    public void displayInfo() // this is where the error is encountered
    {
        System.out.println("Name = "+ name + "Number = "+number+ " Date = " + date + "Sallary = "+ calculateSalary());
                    
    }


}

Upvotes: 0

Views: 77

Answers (3)

yafiet andebrhan
yafiet andebrhan

Reputation: 191

since number is declared as private it is not visible you have to use your getNumber() method

Upvotes: 0

user11760346
user11760346

Reputation:

EDIT: I actually take back my answer below. That will only work if the classes are in the same file like this:

class OuterClass
{
     public class Employee
     {
          // ...
     }

     public class ProductionWorker extends Employee
     {
          // ...
     }
}

You can either change the variables visibility to public/protected or use the getter methods you defined in your Employee class. I'd recommend using the getter methods.


In order to access the values of the Employee class in your subclass ProductionWorker, you have to use the super keyword.

public void displayInfo()
{
     System.out.println("Name = " + super.name + "Number = " + super.number + " Date = " + super.date + "Salary = " + calculateSalary());
}

Upvotes: 0

Scary Wombat
Scary Wombat

Reputation: 44813

As the below fields are private

private String name;
private String number;
private String date;

you do not have access to them in the subclass, so either make them protected or use the getter methods.

Upvotes: 1

Related Questions