130 likes | 281 Views
Overloading Methods & Constructors. Overloaded Method: When multiple methods in a class have the same name, but use different types of parameters. For Example, you might find both of these in the Rectangle class: public void setLength(double dblPLength) { dblLength = dblPLength; }
E N D
Overloaded Method: • When multiple methods in a class have the same name, but use different types of parameters.
For Example, you might find both of these in the Rectangle class: public void setLength(double dblPLength) { dblLength = dblPLength; } public void setLength(String strPInput) { dblLength = Double.parseDouble(strPInput); } This one sets dblLength from an argument that is a number. This one sets dblLength from an argument that is a String.
One of the methods accepts an argument as a Double data type. • One of the methods accepts a String, and then parses that String to a double data type. • Both of the methods in the example do the same thing – they set the length.
Why would we have two methods with the same name that do the same thing? • An object’s purpose is to provide a specific service. • Because the Rectangle object has overloaded methods it is much more flexible. • Programs with dialog boxes can use the method (without having to parse the data from the dialog box). • Programs that use the command prompt can use the method (and send the data directly as a number to the method.)
Binding: • The process of matching a method call to the correct method. • Java uses the method’s name and parameter list to determine which method to “bind” the call to.
Binding Example: • Here is a method call from the Main class: box.setWidth(strInput); • Which method will Java “bind” the method call to? public void setLength(double dblPLength) { dblLength = dblPLength; } OR public void setLength(String strPInput) { dblLength = Double.parseDouble(strPInput); }
Binding Example: • Here is a method call from the Main class: box.setWidth(strInput); • Which method will Java “bind” the method call to? public void setLength(double dblPLength) { dblLength = dblPLength; } OR public void setLength(String strPInput) { dblLength = Double.parseDouble(strPInput); } Java will bind the method call to this version of the setLength method.
Constructors can also be overloaded. • This means that a class can have more than one constructor. • For example, we might want to give programmers the opportunity to: • set the values of a rectangle object when it is created. OR • Not set the values of a rectangle object when it is created and give it pre-defined values.
Example of an Overloaded Constructor: public Rectangle() { dblLength = 1.0; dblWidth = 1.0; } public Rectangle(Double dblPLength, Double dblPWidth) { dblLength = dblPLength; dblWidth = dblPWidth; } No-Argument Constructor. Pre-defined Constructor.
Now the user can use either of the following when creating a Rectangle Object: Rectangle box1 = new Rectangle(); This creates a rectangle object with the values of 1 for the length and width. Rectangle box2 = new Rectangle(8.0, 7.0); This creates a rectangle object with the values of 8 for the length and 7 for the width.
By making overloaded constructors and methods we make our classes: • useful to other programs that we may not create. • flexible so programmer’s don’t always have to parse data before sending arguments to the object’s methods.