Select Page

Welcome to “Java Diaries” a series in which I document my self-teaching of Java as both a record of what I’ve done and a reference for me to check back on later – hopefully aiding the information I’ve been consuming to sink in. Click here to view all posts in this series.

When Method Overloading was first covered in the Udemy course I am currently following my initial reaction was “Eh? Why would I use this?” but now I understand the power of method overloading I totally understand why we should be using it. But first – what is method overloading?

Method Overloading is a feature that allows a class to have more than one method having the same name, if their argument lists are different. –  Beginnersbooks.com

As noted, when you use method overloading you create a new method using the same name but you change the parameter list of the method (also known as the method signature).

public static void sumOfNumbers (int num1, int num2) {
    int totalSum = num1 + num2;
    System.out.println("The sum is " + totalSum);
}

public static void sumOfNumbers (int num1, int num2, int num3) {
    int totalSum = num1 + num2 + num3;
    System.out.println("The sum is " + totalSum);
}

Doing things in this way allows you to keep your method names organised and if you have a few methods that do the same task, but slightly different each time, it makes it easy to remember what method does what. In the example above we create a method called sumOfNumbers with a signature of two ints: num1, num2. We then add num1 and num2 together and print the line “The sum is X” with X being the result of the calculation.

Underneath this we create another method with the same name sumOfNumbers except this time we have three ints: num1, num2, num3. We then calculate the total of all three of these numbers and display the result. This is a very simple case of method overloading but you can see how it can come in handy. We have a method which is the sum of a set of numbers, instead of creating two methods (for example sumOfTwoNumbers, sumOfThreeNumbers) we can use the same name and we know when reading the code back exactly what the code does and the signature shows what can be passed to the methods.

Challenge section header

After being taught the concepts of Method Overloading it was time for a challenge on the Udemy course. This time Tim asked us to create a program that would convert Feet and Inches into Centimeters.

Once I wrapped my head around the calculations needed to convert feet and inches to centimeters this challenge was a lot simpler than previous ones. I believe in large part this is because I’m finally understanding some of the concepts I was struggling with (such as the % remainder operator).

public static double calcFeetAndInchesToCentimeters (double feet, double inches) {

    double centimeters;

    if ((feet >= 0) && (inches >=0 && inches <=12)) {
        centimeters = (feet * 12 + inches) * 2.54;
        System.out.println(feet + " feet, " + inches + " in = " + centimeters + " cm." );
        return centimeters;
    } else {
        System.out.println("Invalid parameters");
        return -1;
    }

}

public static double calcFeetAndInchesToCentimeters (double inches) {

    double feet = (int) inches / 12;
    double remainingInches = (int) inches % 12;

    if (inches >=0) {
        System.out.println(inches + " inches converts to " + feet + " feet, " + remainingInches + " inches.");
        return calcFeetAndInchesToCentimeters(feet, remainingInches);
    } else {
        System.out.println("Invalid parameters");
        return -1;
    }

The above isn’t the most optimal way of completing the challenge apparently – in an ideal world you would do the validation check first to save computing time (why calculate it if it isn’t valid in the first place?) but this is how I completed it.

In the first method calcFeetAndInchesToCentimeters I created two parameters, double feet and double inches.

We then converted this to centimeters using the calculation: (feet * 12 + inches) * 2.54;

After this, we printed a line with the conversion in it and also returned the centimeters value back to the method.

In the second method, also called calcFeetAndInchesToCentimeters I used only one variable this time, double inches. Before I can calculate how many centimeters those inches convert to I need to convert them into feet and inches so I can use these in my first method. This is done by first dividing the number of inches by 12, to get the feet calculation. I then use the remainder operator to find out how many inches are left after completing the conversion to feet. These are stored in a variable named “remainingInches“.

I then call the first method, which accepts two parameters, feet and inches, and pass the feet and remainingInches variables from within our second method. This then puts those values into the first method and the first method runs as if we had used that method on its own.

Hopefully you can see from this that method overloading is a great skill to have when learning Java as not only does it improve code readability it prevents us having to learn multiple method names. If you have struggled to understand Method Overloading I recommend going back to the previous post and brushing up on methods again and then come back and read through this again slowly and study the code examples. This is one of these ideas that may not come across well when written down but clicks when you actually practice.

Thanks for reading, and see you in the next one!

Next Post ButtonLast Post Button