Java: Overloading (George Foreman Edition)

In this post, I’ll be discussing Overloading in Java Programming, using the Feynman Technique with the help of boxing legend George Foreman’s five sons. This post is part of the Feynman/20-hour challenge. For more information, click here.

What is Overloading in Java?

Overloading in Java programming is when a bunch of constructor methods (a container that holds information or instructions) have the same name within a program. In most programming languages, this is an absolute no-no. Names are used to activate or define what’s inside the object, to tell your program which is which, however, Java seems to have no problems telling their twins apart.

The best way I can explain this is with the help of George Foreman’s five sons, all of which are named “George Edward”, after their father.

You Talking the George Foreman Grill Guy?

For those that don’t know who I’m talking about, George Senior is a two-time world heavyweight boxing champion and Olympic gold medalist. He has been inducted into the World Boxing Hall of Fame and the International Boxing Hall of Fame. George is considered by many as being one of the 10 greatest boxers of all time. And yes, he is THAT George Foreman grill guy. Needless to say, the man is the “ish”.

George has twelve children, but for our topic, we’ll need help from his sons: George Edward II, George Edward III, George Edward IV, George Edward V, and George Edward VI.

Don’t believe me, look it up.

Calling For George

As mentioned before, in overloading, constructors can have the same name. They are differentiated based on the number of parameters (items listed within the “( )”. Here’s an example below:

public WhichGeorge(String a, String b, String c, String d, String e)
{
seniority = a;
boxer = b;
television = c;
titles = d;
children = e;
}
public WhichGeorge(String b, String c, String d, String e)
{
seniority = "";
boxer = b;
television = c;
titles = d;
children = e;
}
public WhichGeorge(String b, String c, String e)
{
seniority = "";
boxer = b;
television = c;
titles = "";
children = e;
}
public WhichGeorge(String b, String c)
{
seniority = "";
boxer = b;
television = c;
titles = "";
children = "";
}
public WhichGeorge(String c)
{
seniority = "";
boxer = "";
television = c;
titles = "";
children = "";
}

Each “WhichGeorge” has the same assignment statements (stuff inside the “{ }”). In each variation, one gets emptied, leaving a default (empty ” “) value. As that’s happening, the parameters are also being emptied out. In the end, only String c remained.

For the sake of sanity, I used the same assignment statements, but each can hold different statements than the other.

In these assignment, we ask:

  • the seniority,
  • if they’re a boxer,
  • been on television,
  • had any titles,
  • and have children.

With this breakdown, it’s easier to figure out “WhichGeorge” each constructor was referring to.

This is how the Java compiler (the system that runs the code) figures out which constructor is which.

…but for the sake of your sanity, don’t name every one of your children by the same name. Life is hard enough as it is. Don’t make yours any harder.

Click below for more info on the Overloading technique in Java Programming: