Search code examples
javapolymorphism

How to easily make a large number of polymorphic assignments?


Currently this is how I do my polymorphic assignments, manually, one by one:

Eg:

employees[ 0 ] = new salariedEmployee();
employees[ 1 ] = new salariedEmployee();
employees[ 2 ] = new commissionEmployee();
employees[ 3 ] = new hourlyEmployee();

How do you assign it more efficiently? Let's say you have to make 20 assignments?


Solution

  • Try this.

    employee[] employees = {
        new salariedEmployee(),
        new salariedEmployee(),
        new commissionEmployee(),
        new hourlyEmployee(),
    };