Home > Net >  Use inheritance as subcategory
Use inheritance as subcategory

Time:11-23

Let's say I have a parent class Employee and three subclasses:

  • Worker ( which have a hourlySalary attribute)
  • PermanentEmployee ( Which have a monthlySalary attribute)
  • Manager ( which have a monthlySalary and a Comission attribute)

Each category does have his own method to calculate the monthly salary.

What would be the best way if I want to "convert" a Worker into a PermanentEmployee... if both are own classes and inheritance from the Employee class...

I would implement a method which overgive or delete the attribute which is needed but this sounds very circuitously. Is there any better way? At the end I need to use inheritance for my study project.

Thank you for your hints.

Best Regards Jake

CodePudding user response:

Why not just have both types of pay fields in Worker, where you can set either to 0? This way you can reduce the number of classes you need and simplify your logic.

For myself, I think about how I would model this in a (SQL) database, and this is how I would do it. The more classes you maintain, the more headaches you'll have when the project requirements change

BTW after submitting this answer I am going to vote to close this question as opinion based. I think this question could have many valid answers

CodePudding user response:

public abstract class Employee {

    // attribut definition
    private StringProperty firstname;
    private StringProperty lastname;
    private ObjectProperty<LocalDate> bday;
    private ObjectProperty<Gender> gender;
    private StringProperty jobTitle;
    private ObjectProperty<LocalDate> entryDate;
    private IntegerProperty workload;

    // attribute definition OOP2
    private IntegerProperty personnelnr;
    private StringProperty street;
    private StringProperty postCode;
    private StringProperty location;
    private StringProperty mail;
    private StringProperty phone;
    private StringProperty iban;
    private ObjectProperty<EmployeeCategory> employeeCategory;

// Constructor.. Getters and Setters.. 
}
public class Manager extends Employee {
    // attribute definition
    private DoubleProperty monthlySalary;
    private DoubleProperty commission;


// calculateMonthlySalary(){...};

// Constructor.. Getters and Setters.. 
}
public class Worker extends Employee {
    
    // attribute definition
    private DoubleProperty hourlySalary;



// calculateMonthlySalary(){...};

// Constructor.. Getters and Setters.. 
}
public class PermanentEmployee extends Employee {
    // attribute definition
    private DoubleProperty monthlySalary;


// calculateMonthlySalary(){...};

// Constructor.. Getters and Setters.. 
}
  • Related