Menu
Coddy logo textTech

Recap - Employee Hierarchy

Part of the Object Oriented Programming section of Coddy's Dart journey — lesson 42 of 110.

challenge icon

Challenge

Easy

Let's build a complete employee hierarchy system that brings together all the inheritance concepts from this chapter. You'll create a base employee class and extend it with specialized roles, each with their own unique behaviors.

You'll organize your code into two files:

  • employee.dart: Define your employee hierarchy here:
    • An Employee base class with String name, double salary, and int id properties. Include a constructor and a method getDetails() that returns '[name] (ID: [id]) - Salary: $[salary]'
    • A Manager class that extends Employee and adds an int teamSize property. Use super parameters for the inherited fields. Override getDetails() to return the parent's details followed by ', Team: [teamSize] members'
    • A Developer class that extends Employee and adds a String language property. Override getDetails() to return the parent's details followed by ', Specializes in [language]'
    • A TeamLead class that extends Developer and adds an int projectCount property. Override getDetails() to build upon the Developer's version by appending ', Leading [projectCount] projects'
  • main.dart: Import your employee file and create instances of each employee type:
    • Create an Employee with name 'Alice', salary 50000.0, and id 101
    • Create a Manager with name 'Bob', salary 75000.0, id 102, and teamSize 8
    • Create a Developer with name 'Carol', salary 65000.0, id 103, and language 'Dart'
    • Create a TeamLead with name 'David', salary 80000.0, id 104, language 'Python', and projectCount 3
    • Print the result of calling getDetails() on each employee, one per line

Notice how TeamLead demonstrates multi-level inheritance - it extends Developer, which extends Employee. Each level adds its own details while preserving the information from parent classes using super.

Expected output:

Alice (ID: 101) - Salary: $50000.0, Team: 8 members
Bob (ID: 102) - Salary: $75000.0, Team: 8 members
Carol (ID: 103) - Salary: $65000.0, Specializes in Dart
David (ID: 104) - Salary: $80000.0, Specializes in Python, Leading 3 projects

Try it yourself

import 'employee.dart';

void main() {
  // TODO: Create an Employee with name 'Alice', salary 50000.0, and id 101

  // TODO: Create a Manager with name 'Bob', salary 75000.0, id 102, and teamSize 8

  // TODO: Create a Developer with name 'Carol', salary 65000.0, id 103, and language 'Dart'

  // TODO: Create a TeamLead with name 'David', salary 80000.0, id 104, language 'Python', and projectCount 3

  // TODO: Print getDetails() for each employee, one per line
}

All lessons in Object Oriented Programming