Wednesday, 5 February 2025

Define an abstract class staff with protected members id and name. Define a parameterized constructor. Define one subclass office staff with a member department. Create n objects of office staff and display details


import java.util.Scanner;


abstract class Staff {

    protected int id;

    protected String name;


    // Parameterized constructor

    public Staff(int id, String name) {

        this.id = id;

        this.name = name;

    }


       public abstract void display();

}



class OfficeStaff extends Staff {

    private String department;


    // Parameterized constructor

    public OfficeStaff(int id, String name, String department) {

        super(id, name);  // Call to the superclass (Staff) constructor

        this.department = department;

    }


    // Implement the display method

       public void display() {

        System.out.println("ID: " + id);

        System.out.println("Name: " + name);

        System.out.println("Department: " + department);

    }

}



public class Main {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter the number of office staff members: ");

        int n = scanner.nextInt();

        scanner.nextLine(); // Consume the newline character


        // Array to hold OfficeStaff objects

        OfficeStaff[] staffArray = new OfficeStaff[n];


        // Taking input for each office staff

        for (int i = 0; i < n; i++) {

  System.out.println("\nEnter details for staff member " + (i + 1) + ":");

            System.out.print("Enter ID: ");

            int id = scanner.nextInt();

            scanner.nextLine(); // Consume the newline character

            System.out.print("Enter Name: ");

            String name = scanner.nextLine();

            System.out.print("Enter Department: ");

            String department = scanner.nextLine();


   staffArray[i] = new OfficeStaff(id, name, department);  // Create new OfficeStaff object

        }


        // Displaying details of all office staff members

        System.out.println("\nDetails of Office Staff Members:");

        for (int i = 0; i < n; i++) {

            staffArray[i].display();

            System.out.println("-----------------------------");

        }

        scanner.close();

    }

}