Skip to content

4 Inheritance with Employee and Freelancer

  1. If needed, retrieve the OOP Company project up to chapter 3 from DLO. It contains the Program, Person, and Department classes as you left them at the end of chapter 3.

  2. The company recognises two types of persons: employees and freelancers. In this exercise these become subclasses of the Person class. The Person class must also be adapted.

    Mermaid diagram Mermaid diagram

  3. Add the ToString() method to the Department class.

    1. The desired result is "department name at location" with the attribute values in place of the italics.
  4. Modify the Person class according to the class diagram:

    1. Pay attention to visibility modifiers.
    2. Use a constant for the default value of the name attribute.
    3. Adjust the constructors and keep applying constructor chaining.
    4. The CalculateYearlyIncome() method always returns 0.
    5. Remove properties of removed attributes.
    6. Add the ToString() method.
      1. The desired result is "name lives in city and works at department name at location".
      2. Use the ToString() method of the Department class!
  5. Create the Employee subclass according to the class diagram:

    1. Create constructors and apply constructor chaining. Use the known default values.
    2. Use the constructors of the Person class.
    3. Create the properties.
    4. Yearly income is 12 times monthly salary plus any bonus. The bonus equals monthly salary.
    5. Add the ToString() method.
      1. The desired result is "name lives in city and works at department name at location and is an employee with/without right to a bonus".
      2. Use the ToString() method of the Person class!
      3. The "with/without" text is determined by the HasRightToBonus() method.
  6. Create the Freelancer subclass according to the class diagram:

    1. Use the constructors of the Person class. The default value for hoursWorked is 0.
    2. Create the properties.
    3. Yearly income is hourly rate times hours worked.
    4. Hours worked is increased when the freelancer is hired via the Hire() method.
    5. Add the ToString() method.
      1. The desired result is "name lives in city and works at department name at location and is a freelancer with an hourly rate of hourlyRate".
      2. Use the ToString() method of the Person class!
  7. Adjust your Main method as follows:

    1. Create an array named departments with 4 departments:
      1. The department Operations in Hilversum;
      2. The department Support in Amsterdam;
      3. The department Management in Almere;
      4. The department Documentation in Gouda.
    2. Create an employee named boss (name: Mark, city: Den Haag, monthly salary: 10000, department: Management). Note: use the array for the department.
    3. Create an employee named employee (name: Caroline, city: Delft, monthly salary: 4000, department: Support);
    4. Create a freelancer named assistant (name: Klaas, city: Diemen, hourly rate: 50,00, department: Documentation);
    5. Hire Klaas for 160 hours.
    6. Add code so that your output looks like this. Use the ToString() method (implicitly):
    1
    2
    3
    4
    The number of people in the company is 3
    Mark lives in Den Haag and works at department Management at Almere and is an employee with right to a bonus
    Caroline lives in Delft and works at department Support at Amsterdam and is an employee without right to a bonus
    Klaas lives in Diemen and works at department Documentation at Gouda and is a freelancer with an hourly rate of 50.0
    
    1. Add code so that the following appears below the output above:
    1
    2
    3
    Mark earns 130000,00 per year
    Caroline earns 48000,00 per year
    Klaas earns 8000,00 per year