Recap - Employee Hierarchy
Part of the Object Oriented Programming section of Coddy's Java journey — lesson 27 of 87.
Challenge
EasyLet's build a complete employee hierarchy system that brings together all the inheritance concepts from this chapter. You'll create a company structure where different types of employees share common attributes but have their own specialized behaviors.
You'll organize your code across four files:
Employee.java: Create the base class that all employees share. Every employee has a name and salary. Include a constructor that initializes both fields and a methodgetDetails()that returns:[name] earns $[salary]. Also add a methodwork()that prints:[name] is working.Manager.java: Create a class that extends Employee. Managers have an additionalteamSizefield representing how many people they manage. Usesuperto handle the parent's initialization. Override thework()method to print:[name] is managing a team of [teamSize]. Add a methodgetManagerDetails()that returns:Manager: [name] earns $[salary], Team size: [teamSize].Developer.java: Create another class that extends Employee. Developers have alanguagefield for their programming specialty. Again, usesuperfor the parent portion. Overridework()to print:[name] is coding in [language]. Add a methodgetDeveloperDetails()that returns:Developer: [name] earns $[salary], Language: [language].Main.java: Bring everything together here. You'll receive five inputs: an employee name, salary, team size for the manager, another name, and a programming language for the developer. Create a Manager and a Developer, then for each one call their specialized details method followed by theirwork()method.
You will receive five inputs in this order: manager name (String), manager salary (double), team size (int), developer name (String), and programming language (String). Use a salary of 75000.0 for the developer.
Format all salary values with 2 decimal places using String.format("%.2f", salary). Your output should show four lines total - the manager's details and work behavior, followed by the developer's details and work behavior.
Try it yourself
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Read inputs
String managerName = scanner.nextLine();
double managerSalary = scanner.nextDouble();
int teamSize = scanner.nextInt();
scanner.nextLine(); // consume newline
String developerName = scanner.nextLine();
String language = scanner.nextLine();
// Developer salary is fixed at 75000.0
double developerSalary = 75000.0;
// TODO: Create a Manager object with managerName, managerSalary, and teamSize
// TODO: Create a Developer object with developerName, developerSalary, and language
// TODO: Print manager's details using getManagerDetails()
// TODO: Call manager's work() method
// TODO: Print developer's details using getDeveloperDetails()
// TODO: Call developer's work() method
}
}
All lessons in Object Oriented Programming
1Fundamentals of OOP
External FilesIntroduction to OOPClasses vs ObjectsThe this KeywordMethodsFields (Attributes)Constructor MethodConstructor OverloadingRecap - Simple Calculator4Inheritance
Basic Inheritance (extends)The super KeywordMethod Overriding (@Override)Constructor ChainingThe Object ClassSingle & Multilevel InheritWhy No Multi Class InheritRecap - Employee Hierarchy7Special Methods & Object Class
toString() Methodequals() and hashCode()clone() MethodcompareTo() and ComparableComparator InterfaceRecap - Custom Sorting2Access Modifiers & Encapsulate
Access Levels OverviewGetter and Setter MethodsInformation HidingThe final KeywordRecap - Bank Account Manager5Polymorphism
Method Overloading BasicsMethod Overriding (Run-Time)Upcasting and DowncastingThe instanceof OperatorAbstract Classes and MethodsRecap - Shape Calculator8Advanced OOP Concepts
Composition vs InheritanceAggregation vs CompositionInner Nested & Anonymous ClassEnums and Enum MethodsRecords (Java 16+)Sealed Classes (Java 17+)3Class Props & Static Member
Instance vs Static VariablesStatic MethodsStatic BlocksConstants (static final)Recap - Counter & Utility6Interfaces & Abstract Classes
Introduction to InterfacesImplementing InterfacesMulti Interface ImplemenDefault & Static in InterfaceAbstract Classes vs InterfacesFunctional InterfacesRecap - Payment System