concept overloaded method in category java

appears as: overloaded methods, overloaded methods, n overloaded method, overloaded method, An overloaded method
Java SE 11 Programmer I Certification Guide MEAP V03

This is an excerpt from Manning's book Java SE 11 Programmer I Certification Guide MEAP V03.

The insert method is as powerful as the append method. It also exists in multiple flavors (read: overloaded methods) that accepts many data types. The main difference between the append and insert methods is that the insert method enables you to insert the requested data at a particular position, but the append method allows you to add the requested data only at the end of the StringBuilder object:

8.2      Create and invoke overloaded methods

[7.2] Create and invoke overloaded methods

Overloaded methods are methods with the same name but different method parameter lists. In this section, you’ll learn how to create and use overloaded methods.

This second approach, providing one set of instructions (with the same name) but a different set of input values can be compared to using overloaded methods in Java, as shown in figure 8.9.

Figure 8.9 Real-life examples of overloaded methods

Again, overloaded methods are methods that are defined in the same class with the same name, but with different method argument lists. As shown in figure 8.9, overloaded methods make it easier to add methods with similar functionality that work with different sets of input values.

The preceding code is an example of the simplest flavor of overloaded methods. You can also define overloaded methods in which the difference in the argument list is in the types of the parameters that are accepted:

double calcAverage(int marks1, double marks2) {  #A
    return (marks1 + marks2)/2.0;
}
double calcAverage(char marks1, char marks2) {   #B
    return (marks1 + marks2)/2.0;
}

But you can’t define overloaded methods by just switching an array parameter into a vararg or vice versa (unless the vararg or array item type doesn’t remain the same). Behind the scenes, varargs are implemented as arrays. So the following overloaded methods won’t compile:

Get Programming with Java MEAP V04 livebook

This is an excerpt from Manning's book Get Programming with Java MEAP V04 livebook.

Now that we have created the overloaded methods, the next step is to call these methods. This is where the real value of overloaded methods comes into play. When an object is instantiated in a class that has overloaded methods, the correct method is determined by the quantity, data type and order of the argument list. 

Let’s use the Address class in Listing 13.1 as a starting point for examples on calling the overloaded methods.  There are three versions of the print method, each overloaded by changing the parameter list.  In the main method, I will create an Address object and then use the print method to print three different types of addresses.  Figure 13.5 shows sample output from executing the code in Listing 13.3.

Listing 13.1 Code for overloading three print methods to print an address
1     public class Address {
 2         public void print(String fName, String lName, String   //#A
 3                 address1, String city, String state, String zip) {
 4              //code to print address omitted
 5         }
 6         public void print(String address1, String address2, String city, String state,
 7                 String zip) { //#B
 8              //code to print address omitted 
 9         }
 10        public void print(String fName, String lName, int POBox,
 11               String city, String state, String zip) {
 12             //code to print address omitted     
 13        }
 14    }

Figure 13.5 Sample output printing three addresses using overloaded methods

Listing 13.2 Complete program to print three addresses using overloaded methods
1     public class Address {  
 2         public void print( String fName, String lName, String
 3                 address1, String city, String state, String zip) {
 4             System.out.println(fName + " "+lName+"\n"+address1 +
 5                    "\n"+city+", "+state+" "+zip+"\n");     
 6         }
 7         public void print(String address1, String city, String state, String zip) {
 8             System.out.println("Current Resident\n"+address1 +
 9                     "\n"+city+", "+state+" "+zip+"\n");
 10        }
 11        public void print(String fName, String lName, int POBox, String city,
 12                String state, String zip) { 
 13            System.out.println(fName + " "+lName+"\nPO Box "+POBox +
 14                    "\n"+city+", "+state+" "+zip+"\n");
 15        }
 16        public static void main(String[] args){
 17            Address address1 = new Address();    //#A
 18            address1.print("123 First Street", "Carpinteria", "CA", "23901"); //#B
 19            address1.print("Peggy" ,"Fisher", "123 Main Street", "Ambler",     //#C
 20                "PA", "19001");
 21            address1.print("Tish","Macclay", 756, "Scituate", "MA", "02566"); //#D
 22        }
 23    }

This lesson reviewed how to write overloaded methods in Java. An overloaded method is useful when your class requires a method that can have different arguments in the parameter list.  For code readability, it is helpful to use the same method name, but the parameter lists must be different.  For example, a method used to calculate the average of three numbers might have one version that accepts all integer values and a second version that has all double values as arguments.  In the next lesson, I introduce another topic related to methods, overriding methods.

OCA Java SE 8 Programmer I Certification Guide

This is an excerpt from Manning's book OCA Java SE 8 Programmer I Certification Guide.

Purpose: To remind you to be careful with the overloaded methods of the class String that accept either char or String or both, the code in this exercise passes an invalid method argument—a char—to method startsWith.

3.4. Create an overloaded method

[6.1] Create methods with arguments and return values; including overloaded methods

Overloaded methods are methods with the same name but different method parameter lists. In this section, you’ll learn how to create and use overloaded methods.

This second approach, providing one set of instructions (with the same name) but a different set of input values can be compared to using overloaded methods in Java, as shown in figure 3.17.

Again, overloaded methods are methods that are defined in the same class with the same name, but with different method argument lists. As shown in figure 3.17, overloaded methods make it easier to add methods with similar functionality that work with different sets of input values.

Figure 3.17. Real-life examples of overloaded methods
sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage
test yourself with a liveTest