Method Overloading in Java

Learn method overloading in java with simple example.

Method Overloading

Having multiple methods with same name in java programming is known as Method overloading. It is also known as compile time polymorhism in java.

In other word, a class has multiple methods with same name. For example, below class “Paint” has three methods with same name i.e. “Color”. Notice that all the methods have different number of arguments.

Note that if all methods with same name have same number of arguments, then there data types must be different. Method overloading same as constructor overloading in java.

class Paint{
	
//all methods have same name accepting different
//color code
 public void Color(int x) {}
 public void Color(int x,int y) {}
 public void Color(int x, int y, int z) {}
}

 

Definition: “Multiple methods in a class with same name having different parameters or data types is known as method overloading”.

You can see here a screen shot taken from eclipse for String class valueOf() overloaded method in java framework.

java method overloading
java method overloading

Think, if valueOf method wouldn’t have overloaded, then it would have looked like below, which is not clean, ugly and more difficult to read than above overloaded one.

		String valueOfBoolean(boolean arg0)
		String valueOfChar(char arg0)
		String valueOfCharArray(char[] arg0)
		String valueOfDouble(double arg0)
		String valueOfFloat(float arg0)
		String valueOfInt(int arg0)
		String valueOfLong(long arg0)

 

Java Method Overloading Example

Below class Painting has three overloaded methods named color.

package programs;

class Painting {

	// instance variables.
	String color1;
	String color2;
	String color3;
	String byDefault = "black";

	// Three parameterized method that called by main method.
	public void color(String color1, String color2, String color3) {

		// initializing variables.
		this.color1 = color1;
		this.color2 = color2;
		this.color3 = color3;

		// calling two parameterized method.
		this.color(color1, color2);

		System.out.println("Color1= " + color1 + " Color2= " + color2 + " Color3= " + color3);

	}

	// Two parameterized method is called.
	public void color(String color1, String color2) {

		// initializing variables.
		this.color1 = color1;
		this.color2 = color2;

		// calling one parameterized constructor.
		this.color(color1);

		System.out.println("Color1= " + color1 + " Color2= " + color2);

	}

	// One parameterized method is called.
	public void color(String color1) {

		// initializing variables.
		this.color1 = color1;

		// calling default parameterized method.
		this.color();

		System.out.println("Color1= " + color1);

	}

	// Default parameterized method is called.
	public void color() {

		// After print, that will return same way that way it comes.
		System.out.println("Default-color= " + byDefault);

	}
}

public class TestColorPainting {

	public static void main(String[] args) {

		// object is created
		Painting painting = new Painting();

		// calling two parameterized constructor
		painting.color("green", "yellow", "blue");

	}

}

Output:
Default-color= black
Color1= green
Color1= green Color2= yellow
Color1= green Color2= yellow Color3= blue

NOTE:

Method overloading comes under compile time polymorphism. Recommend to read why method overloading is called compile time polymorphism. In the link, both run time and compile time has been explained with simple example.



Leave a Comment