One of the key concepts of Java Object Oriented Programming is Polymorphism, which we will discuss in a later topic. The concept of method overloading is a perfect example of Polymorphism.
You are probably already familiar with the concept of constructors in Java and the fact you can have multiple constructors with the same name as long as the arguments are different. That is Polymorphism.
Java allows for multiple method with the exact same name as long as the arguments they take are different in some way. Again, we are talking abut different arguments and not just a different return type. For ease of understanding, all of the overloaded method examples below will all have a return type of void.
Let’s start with some example of overloading a constructor of a sample class. We have overloaded the constructor below for our JavaOverloadingClass. All of the constructors have the exact same name, but all of the input or variables are different. This is all good in Java.
public class JavaOverloadingClass
private String myName;
private int myCounter;
//Constructor that takes single String
public JavaOverloadingClass(String str)
myName = str;
//Constructor that takes String and an Integer
public JavaOverloadingClass(String str, int ctr)
myCounter = ctr;
Let’s go through the method overloading example below. We have a simple class that defines four versions of the same method called overloadedMethod(). The first version is empty in that it takes no arguments. The second version takes a single boolean data type. The third version of the method takes a single String. The last version takes a single integer. It all works and the Java compiler will not complain one bit because this is Polymorphism in action.
public class JavaOverloadingSample
private static boolean myDecision;
private static String myMessage;
private static int myCounter;
public static void main(String args)
//Call the empty version
//Call the version that takes a boolean
//Call the version that takes a String
overloadedMethod("JAVA IS KING");
//Call the version that takes an integer
public static void overloadedMethod()
//Same Method that takes a boolean
public static void overloadedMethod(boolean b)
myDecision = b;
//Same Method that takes a String
public static void overloadedMethod(String msg)
myMessage = msg;
//Same Method that takes a integer
public static void overloadedMethod(int ctr)
myCounter = ctr;
Method Overloading Done Wrong
If you define two methods with the same name and the same arguments, Java will give you a Duplicate Method error message and pretty much slap you upside your head for not understanding Polymorphism in Java. Don’t forget that the method return type has no bearing on method overloading.
The Java programming language supports method overloading, which is pretty darn cool. Java is able to distinguish between methods with different method signatures. Overloaded methods in Java are different due to the number and type of arguments passed into the method. Hope you enjoyed it and get busy overloading some methods.