-2

I am creating a simple Employee class with Production Worker as a child class using Java. I've got both class fields set to private, but I just would like to know if they need to be public or not. I know that when using private fields it keeps it private from other super classes but does it work the same for inheritance? Thank you in advance for helping out. Code below

package Employee;



public class Employee {
    
    // declare employee class fields
    private String empName;
    private String empId;
    private String hireDate;
    
    // default constructor
    public Employee()
    {
        empName = "";
        empId = "";
        hireDate = "";
        
    }
    // constructor with arguments passed in
    public Employee(String name, String id, String date)
    {
        empName = name;
        empId = id;
        hireDate = date;
    }
    
    // set methods for employee class
    public void setEmpName(String name)
    {
        empName = name;
    }
    public void setEmpID(String id)
    {
        empId = id;
    }
    public void setHireDate(String date)
    {
        hireDate = date;
    }
    
    // get methods for employee class
    public String getName()
    {
        return empName;
    }
    public String getID()
    {
        return empId;
    }
    public String getDate()
    {
        return hireDate;
    }
    
    // create production worker class; child to employee class
    public class ProductionWorker extends Employee
    {
        private int shift;
        private double payRate;
        // default constructor
        public ProductionWorker()
        {
            shift = 0;
            payRate = 0.00;
        }
        // constructor with args passed in
        public ProductionWorker(int s, double p)
        {
            shift = s;
            payRate = p;
        }
        
        // set methods for the production worker class
        public void setShift(int s)
        {
            shift = s;
        }
        public void setPayRate(double p)
        {
            payRate = p;
        }
        // get methods for the production worker calss
        public int getShift()
        {
            return shift;
        }
        public double getPayRate()
        {
            return payRate;
        }
    }
Fitzgerald
  • 17
  • 7
  • No, no, NO. None of the fields should be public. Make them private with accessor and mutator methods or at most protected – Hovercraft Full Of Eels Mar 23 '21 at 16:14
  • What you're constructing is an inner class, for which some special rules apply. In general I'ld recommend to create a new `.java` file for the child, so it's not an inner class anymore. Child classes can access parent's fields if they're declared as `protected` then. Apart from that, I'ld avoid `public` fields in favor for private/protected fields with public getters. – NotX Mar 23 '21 at 16:20
  • Thank you for the help. Ah yes, good idea making a new file for the child class. Will do that then – Fitzgerald Mar 23 '21 at 16:21
  • @NotX, when i create a separate .java file for my child class, do I still need to use "extends" when declaring the child class? – Fitzgerald Mar 23 '21 at 16:27
  • @Fitzgerald More than ever. How else would the compiler know that a class actually is a child class of another one? It's bad style to use inner classes by default for multple reasons, but avoiding those won't spare you any writing work. – NotX Mar 23 '21 at 16:43

1 Answers1

-1

Make the fields of the parent class protected.

It’s good design practice to keep the state as private as possible.

Bohemian
  • 389,931
  • 88
  • 552
  • 692