Menu
Coddy logo textTech

Recap - Employee Hierarchy

Part of the Object Oriented Programming section of Coddy's Java journey — lesson 27 of 87.

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 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 method getDetails() that returns: [name] earns $[salary]. Also add a method work() that prints: [name] is working.
  • Manager.java: Create a class that extends Employee. Managers have an additional teamSize field representing how many people they manage. Use super to handle the parent's initialization. Override the work() method to print: [name] is managing a team of [teamSize]. Add a method getManagerDetails() that returns: Manager: [name] earns $[salary], Team size: [teamSize].
  • Developer.java: Create another class that extends Employee. Developers have a language field for their programming specialty. Again, use super for the parent portion. Override work() to print: [name] is coding in [language]. Add a method getDeveloperDetails() 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 their work() 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