Home > Tutorial > Java Method Overloading Examples

Java Method Overloading Examples

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.


Constructor Overloading

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.

package com.jcd.java.tutorials.MavenProject;

public class JavaOverloadingClass 
{
	private String myName;
	private int myCounter;

	//Empty Constructor
	public JavaOverloadingClass() 
	{
		
	}
	
	//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;
	}
}


Method Overloading

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.

package com.jcd.java.tutorials.MavenProject;

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
			overloadedMethod();
			
			//Call the version that takes a boolean
			overloadedMethod(true);
			
			//Call the version that takes a String
			overloadedMethod("JAVA IS KING");
			
			//Call the version that takes an integer
			overloadedMethod(8);
	}
	
	//Empty Method
	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.


Concluding Comments

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.

Smedley
Posted by

Smedley

Smedley has been programming in Java for about twenty years. He enjoys sharing code samples to other fellow Java coders.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

Copyright © 2018 JavaCodeDepot