Overloading in Java

Learn how to overload constructors and methods in Java

In your journey of learning Java make sure to learn about overloading. This concept is used often in Java programming and it is therefore good to know.

In this article I'll address both constructor overloading and method overloading.

Constructor overloading

Let's assume you are in charge of the stock of bread in a bakery. Your role is to keep track of bread types and order stock once it runs low. Customers can buy both sliced and unsliced bread.

We therefore define a Bread class with everything that is needed to model the bakery bread stock. Since we are interested in knowing whether we need more sliced or unsliced bread, we overload the constructor in this example:

public class Bread {

    private int cuts;

    Bread() {
    }

    Bread(int cuts) {
        this.cuts = cuts;
    }

  public void getBreadData() {
        if(cuts !=0)
            System.out.println(cuts);
        else
            System.out.println("There are no cuts");
    }    
}
public class Main

    public static void main(String[] args) {

        Bread unsliced = new Bread();
        Bread sliced = new Bread(1);

        unsliced.getBreadData(); // prints out "There are no cuts"
        sliced.getBreadData(); //prints out: "1"
    }
}

We both have a constructor that takes an argument (int cuts) and a constructor of the same name that does not take any argument. It is often recommended to write no argument constructors as this makes it easier for other programmers to use/extend your code (what if the other programmer does not know that the Bread constructor takes an argument?).

Method overloading

While keeping at the bakery example, we may be interested not only by knowing if bread loafs are cut or not, but we may also be interested in the date of fabrication (in this example a String such as "today"/"yesterday". One way to implement such a method could be to overload the getBreadData method with further arguments:

    public void getBreadData() {
        if(cuts !=0)
            System.out.println(cuts);
        else
            System.out.println("There are no cuts");
    }

    public void getBreadData(String fabricationDate) {
        if(cuts !=0)
            System.out.println(cuts + ". Fabrication date is " + fabricationDate);
        else
            System.out.println("There are no cuts" + ". Fabrication date is " + fabricationDate);
    }

The main class therefore looks like:

public class Main

    public static void main(String[] args) {

        Bread unsliced = new Bread();
        Bread sliced = new Bread(1);

        unsliced.getBreadData(); // prints out "There are no cuts"
        sliced.getBreadData();  // prints out "1"
        unsliced.getBreadData("today"); //prints out "There are no cuts. Fabrication date is today"
        sliced.getBreadData("today"); //prints out "1. Fabrication date is today"
    }
}

So what is the fuss with overloading about?

Overloading offers flexibitly to your code. With Java being a strongly typed programming language, you can implement both constructors and methods with the same name, which take though different arguments and therefore also have different behavior.

Try it out!