This article helps you understand the concept of overloading in Java with some code examples.

 

1. What is Overloading in Java?

In object oriented programming, overloading refers to the ability of a class to have multiple constructors or multiple methods with the same name but different signatures, i.e. arguments list.

Consider the following example:

public class Circle {
	private int radius;
	private int x;
	private int y;

	public Circle() {
		this.x = 10;
		this.y = 10;
		this.radius = 20;
	}

	public Circle(int radius) {
		this.radius = radius;
	}

	public Circle(int radius, int x, int y) {
		this.radius = radius;
		this.x = x;
		this.y = y;
	}
}
Here, the Circle class has three constructors with different arguments (the number of arguments is different). The constructor is said to be overloaded. And the following code creates three new instances of this class based on the three overloaded constructors:

// creates a circle with defaults
Circle circle1 = new Circle();

// creates a circle with R=50 and default center
Circle circle2 = new Circle(50);

// creates a circle with R=100, center(x, y) = (50, 70)
Circle circle3 = new Circle(100, 50, 70);
Consider another example:

public class Graphics {
	public void draw(String text) {
		// draws the text
	}

	public void draw(Circle circle) {
		// draws the circle
	}

	public void draw(Rectangle rect) {
		// draws the rectangle
	}
}
Here, the Graphics class has three versions of the draw() method with different types of arguments (String, Circle and Rectangle). The draw() method is said to be overloaded. And here are the usages of the three overloaded methods:

Graphics graphics = new Graphics();

graphics.draw("Hello");
graphics.draw(new Circle());
graphics.draw(new Rectangle());
So, basically, overloading means providing multiple versions (signatures) of a method by reusing the method name and varying the arguments (either by changing the number of arguments or the types of arguments). The overloaded methods can be declared in the same class, or both inherited by a class, or one declared and one inherited.

 

2. Some Rules about Overloading in Java



In terms of constraints between overloaded methods, overloading is more relaxed than overriding, as the only requirement is to change the arguments, in combination of quantity and types. The return types, access modifiers and throws clauses can be freely declared. To summary, the overloaded methods:

Which overloaded methods to be invoked is decided at compile time, based on the actual number of arguments and the compile-time types of the arguments.

 

3. Java Overloading Examples

Let’s see some more details and complex examples with regard to overloading.

 

public class Shape {

	protected void draw() {
		// draws the shape
	}
}
public class Rectangle extends Shape {

	protected void draw(int x, int y) {
		// draws the rectangle at (x, y)
	}
}
Here, Rectangle is a subclass of Shape so it inherits the draw() method. Therefore, the draw(int x, int y) method in the Rectangle is an overloaded version of the Shape’s draw() method. 

 

 

Related Tutorials:

 

Other Recommended Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube.