Question: How can a method be called for an object that is stored in an array from the main method? I need to call a setter

How can a method be called for an object that is stored in an array from the main method?

I need to call a setter method to change the price variable on an object.

NOTE: this is a normal array, I cannot use an array list!!!

Here is the declaration for the array list within the main method.

Employee[] employeeList = new Employee[10];

Here is a new object that was created.

Hourly hourly1 = new Hourly(1002, "Kelly", "Hostess", 25.75);

This is how the object was added to the list.

employeeList[1] = hourly1;

JAVA

I would like to call this method which exists in a subclass of the employee class, the employee class does not extend the main class.

public void setHourlyRate(double hourlyRate){ this.hourlyRate = hourlyRate; }

If i try to call the method like this it works fine: hourly1.setHourlyRate();

However if I try to call the method with the array value: employeeList[1].setHourlyRate();

I can then only call methods from the employee class, how could I make this work?

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!