1 / 13

Overloading Methods & Constructors

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; }

Download Presentation

Overloading Methods & Constructors

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Overloading Methods & Constructors

  2. Overloaded Method: • When multiple methods in a class have the same name, but use different types of parameters.

  3. 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.

  4. 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.

  5. 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.)

  6. 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.

  7. 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); }

  8. 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.

  9. 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.

  10. 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.

  11. 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.

  12. 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.

  13. UML Diagrams Ex:

More Related