I'm making my first steps in OOP and I'm currently doing some exercises. I've done a simple program with class structure like this (I'll provide only essential parts):
Super class:
public abstract class Employee {
private String lastName;
private float salary;
public Employee(String lastName, float salary) {
this.lastName = lastName;
this.salary= salary;
}
}
And several child classes, all similar to this:
public class Worker extends Employee {
private float hourRate;
public Worker (String lastName, float salary, float hourRate) {
super(lastName, salary);
this.hourRate = hourRate;
}
}
My concern is, when I'll decide to add some field to the Employee class that have to be set in constructor, let's say firstName
, than I have to add it there and in constructor's arguments of all child classes and where I call super()
inside them. It doesn't seem to be the best solution... Is there a better way to do such a thing? Maybe there is some pattern I don't know yet?