Transcripted Summary

There is one more concept that I want to talk to you about in this chapter, and that's overloading methods.

Overloading Methods

Within a class, you can have multiple methods that have the same name but different signatures. And this is known as overloading.

Within this Month class, we're going to create two methods that are called getMonth.

  1. One of the methods is going to take an integer for the month and then return the String value of that month.
  2. Then we'll have an overloaded method that will take the String version of the month and return the integer.
package chapter6;

public class Month {

    public static String getMonth(int month){
            return "";

    public static int getMonth(String month){
            return 0;

    }
}

Okay, notice here — these both have the exact same name.

This is perfectly legal, in fact, you can have however many overloaded methods you want. But they have to differ by the parameter list.

For example, if I change this 1st one to accept a String, then now we see that we have a compilation error.



It's saying that this method is already defined. So, the parameter list must be unique for each of the overloaded methods.

I'm going to quickly type in the real implementation for this.


# Month.java

package chapter6;

public class Month {

    public static String getMonth(int month){
        switch(month){
            case 1: return "January";
            case 2: return "February";
            case 3: return "March";
            case 4: return "April";
            case 5: return "May";
            case 6: return "June";
            case 7: return "July";
            case 8: return "August";
            case 9: return "September";
            case 10: return "October";
            case 11: return "November";
            case 12: return "December";
            default: return "Invalid month. Please enter a value between 1 and 12.";
        }
    }

    public static int getMonth(String month){
        switch(month){
            case "January": return 1;
            case "February": return 2;
            case "March": return 3;
            case "April": return 4;
            case "May": return 5;
            case "June": return 6;
            case "July": return 7;
            case "August": return 8;
            case "September": return 9;
            case "October": return 10;
            case "November": return 11;
            case "December": return 12;
            default: return -1;
        }
    }
}

So, we have two different implementations for this — but what I want you to recall is that we have defined these as static.

In the last section, I told you about how we can call methods that are static from another class without instantiating them.

Let me show you how to do that by executing these.

Let’s create a class called MonthConverter

So, we're going to go ahead and print, and we are going to use the Month class and the “dot”

We're just going to say:

System.out.println(Month.



Notice I can call these two methods without instantiating the Month class because these methods are static.

When I have static methods within a class, there is no need to instantiate that class in order to access them. I can access them simply by using the class name and the dot operator.

  • So, I can just call getMonth(2) — that should print out “February”.
  • And then we'll do another one and this time we'll call the other one getMonth(“January”).

# MonthConverter.java

package chapter6;

public class MonthConverter {

    public static void main(String args[]){
        System.out.println(Month.getMonth(2));
        System.out.println(Month.getMonth("January"));
    }
}

When we run this, we get February and 1, as expected.



Optional Independent Exercise

Here is your optional exercise for this chapter. We want to redo the phone bill calculator to use object-oriented approach this time.



So, a phone bill should have an ID, a base class, a number of allotted minutes and a number of minutes used. And then, it should also be able to calculate the overage, calculate the tax, and calculate the total.

And then, it should also be able to print an itemized bill.

You should also include three constructors

  • a default one
  • one that accepts the ID only
  • one that accepts all fields

Now no matter which of these constructors you use, all fields should be set eventually.

Then you can also create a different class that instantiates the PhoneBill and prints out an itemized bill.

Good luck.

Solution

Programming can be done many different ways, but here’s my solution.



Resources