Recap - Employee Hierarchy
Part of the Object Oriented Programming section of Coddy's Dart journey — lesson 42 of 110.
Challenge
EasyLet'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
Employeebase class withString name,double salary, andint idproperties. Include a constructor and a methodgetDetails()that returns'[name] (ID: [id]) - Salary: $[salary]' - A
Managerclass that extendsEmployeeand adds anint teamSizeproperty. Use super parameters for the inherited fields. OverridegetDetails()to return the parent's details followed by', Team: [teamSize] members' - A
Developerclass that extendsEmployeeand adds aString languageproperty. OverridegetDetails()to return the parent's details followed by', Specializes in [language]' - A
TeamLeadclass that extendsDeveloperand adds anint projectCountproperty. OverridegetDetails()to build upon theDeveloper's version by appending', Leading [projectCount] projects'
- An
main.dart: Import your employee file and create instances of each employee type:- Create an
Employeewith name'Alice', salary50000.0, and id101 - Create a
Managerwith name'Bob', salary75000.0, id102, and teamSize8 - Create a
Developerwith name'Carol', salary65000.0, id103, and language'Dart' - Create a
TeamLeadwith name'David', salary80000.0, id104, language'Python', and projectCount3 - Print the result of calling
getDetails()on each employee, one per line
- Create an
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 projectsTry 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
1Fundamentals of OOP
External FilesLibraries & ImportsIntroduction to OOPClasses vs ObjectsThe this KeywordMethodsInstance VariablesConstructor BasicsRecap - Simple Calculator4Null Safety
Intro to Null SafetyNullable vs Non-NullableThe ? and ! OperatorsLate Keyword & Null SafetyNull-Aware OperatorsNull Safety in ClassesRecap - User Profile System7Abstract Classes & Interfaces
Abstract ClassesAbstract MethodsInterfaces in DartImplicit InterfacesImplementing vs ExtendingMultiple InterfacesRecap - Shape Calculator10Collections & Generics
List, Set, Map OverviewType-Safe CollectionsGeneric ClassesGeneric MethodsGeneric ConstraintsIterable & IteratorRecap - Generic Storage13Advanced OOP Concepts
Composition vs InheritanceExtension MethodsCallable ClassesSealed Classes (Dart 3)Records (Dart 3)Patterns & Matching (3.0)Enums with Methods16Project: Library Management
Project OverviewBook and User Classes2Constructors in Dart
Default ConstructorNamed ConstructorsInitializer ListsConstant ConstructorsFactory ConstructorsRedirecting ConstructorsRecap - Shape Builder5Encapsulation
Public vs Private MembersThe _ Prefix ConventionLibrary-Level PrivacyGetters & Setters DepthInformation HidingRecap - Student Records8Mixins
Introduction to MixinsCreating MixinsUsing Multiple Mixinson Keyword in MixinsMixin vs InheritanceMixin vs InterfaceRecap - Animal System11Special Methods
toString() OverridehashCode & == OverrideComparable Interfacecall() MethodnoSuchMethod OverrideRecap - Custom Collection14Design Patterns Part 1
Intro to Design PatternsSingleton PatternFactory PatternObserver PatternStrategy Pattern3Class Properties
Instance vs Static MembersFinal & Const FieldsLate VariablesStatic Methods & FieldsGetters and SettersRecap - Bank Account Manager6Inheritance
Basic InheritanceThe super KeywordMethod OverridingThe @override AnnotationThe final Class KeywordConstructors & InheritanceRecap - Employee Hierarchy9Polymorphism
Polymorphism BasicsPolymorphism via InterfacesRuntime Type CheckingThe is & as OperatorsCovariant KeywordRecap - Payment Processor