Java Reference
In-Depth Information
For instance, notice in the actionPerformed method that the method variable emp is defined three separate times
and assigned three different Employee objects. We should have created one class variable and Employee object. In
addition, we created several method variables to hold information from the frame. We already had a perfect place to
store this information: the Employee object.
We will change the Employee class to include new properties for pay rate and exemptions and define getter and
setter methods for the properties. We will also allow the user to specify different parameters when instantiating an
Employee object by using a technique called method overloading .
Method overloading takes advantage of the fact that a class can contain many methods with the same name.
Methods within a class must have a unique signature (the method name and expected parameters), not a unique
method name. Therefore, methods can have the same name as long as they expect a different set of parameters.
Method overloading is used to make a class's interface (i.e., public methods) easier to use. For instance, the
String class uses method overloading for the valueOf method. If you look at the String documentation, you will see
several methods named valueOf. However, each valueOf method expects a parameter of a different type (int, double,
long, etc); therefore, each method has a different signature. Overloading makes converting primitives to strings easier
because the programmer only has one method name to remember, valueOf. The JVM determines the correct valueOf
method to execute based on the parameter variable type.
All I can say is, “Thank you, JVM.”
Tutorial: Method Overloading
In Employee, we will create overloaded calculation and constructor methods to make the class easier to use. Yes, you
can overload constructors. Have you noticed how every time we try to create an Employee object, we had to pass five
empty strings? We are going to create a null constructor: a constructor that does not expect any parameters. The null
constructor makes it easier to create an “empty Employee” object but still ensures that the object and class variables
are created.
In Employee:
1.
Add the following statements to define new private class variables:
private int exemptions;
private double payRate;
private String stringTaxAmt;
private String stringGrossSalary;
These class variables will be used for the employee's new properties and take the place of the method variables
used in the calculations.
2.
Click on the first statement you just added to select it.
3.
Click Source then Generate Getters and Setters...
4.
At the “Generate Getters and Setters” window, click on the payRate check box and make
sure that exemptions is selected.
The bottom of the window should say 4 of 16 selected.
5.
Click the OK button.
Four new methods will be generated: two getters and two setters.
6.
Before the constructor, add the following null constructor:
public Employee(){}
 
Search WWH ::




Custom Search