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:

  • Must have different arguments list (quantity and types).
  • May have different return types.
  • May have different access modifiers.
  • May throw different exceptions.
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.

  • Example #1: Overloading a constructor
    public class Dog {
    	String breed;
    	
    	// creates a dog with the default breed
    	public Dog() {
    		this.breed = "Bulldog";
    	}
    	
    	// creates a dog with the specified breed
    	public Dog(String breed) {
    		this.breed = breed;
    	}
    }
  • Example #2: Overloading a method inherited from a superclass
 

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. 

 

  • Example #3: Overloading a method by varying the number of arguments
    public class SortingUtils {
    	void sort(int[] numbers) {
    		// sorts the array into the default ordering
    	}
    	
    	void sort(int[] numbers, int order) {
    		// sorts the array into the specified order
    	}
    	
    	void sort(int[] numbers, int order, int fromIndex, int toIndex) {
    		// sorts array elements ranging from fromIndex to toIndex
    		// into the specified order
    	}
    }
    Here, the overloaded methods provide different choices for the users based on their needs.

  • Example #4: Overloading a method by varying the types of arguments
    public class Line {
    	
    	void draw(int x1, int y1, int x2, int y2) {
    		// draws the line with integer coordinates
    	}
    	
    	void draw(float x1, float y1, float x2, float y2) {
    		// draws the line with real coordinates
    	}
    }
    Here, the overloaded methods have the same number of arguments but different types (int vs. float).

    It’s also possible that the types of the arguments are in the same inheritance tree, as shown in the following example:

    public class ShapeUtils {
    	static void draw(Shape shape) {
    		System.out.println("draws a general shape");
    	}
    	
    	static void draw(Circle circle) {
    		System.out.println("draws a circle");
    	}
    	
    	static void draw(Rectangle rectangle) {
    		System.out.println("draws a rectangle");
    	}	
    }
    Here, the Circle and Rectangle are subclasses of the Shape class.

    Which method is invoked is decided at compile time based on the reference type of the argument. For example:

    Shape circle = new Circle();
    ShapeUtils.draw(circle);
    That will invoke the draw(Shape) method, although the actual object is Circle. Thus this output:

    draws a general shape
    The following code will invoke the draw(Rectangle) method:

    Rectangle rect = new Rectangle();
    ShapeUtils.draw(rect);
    Output:

    draws a rectangle
  • Example #5: Overloading in interfaces and abstract classes.The same principles of overloading are applied for methods declared in interfaces and abstract method in abstract classes. For example:

    public interface Animal {
    	void eat();
    	
    	void eat(String food);
    	
    	void eat(String food, int quantity);
    	
    	void move();
    }
    Here, the eat() method is overloaded with three versions.

    Consider the following abstract class:

    public abstract class AbstractCat implements Animal {
    	
    	abstract void move(int distance);
    	
    }
    Here, the AbstractClass implements the Animal interface, so it inherits the move() method from Animal. Therefore, the move(int distance) method is an overloaded version.

 

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.



Add comment

   


Comments 

#6Neeraj Shukla2019-11-19 22:04
It's very useful to understand different different example easily thanks
Quote
#5Aman2019-03-13 21:39
very well explained
Quote
#4Anitha Siyolisile2018-12-13 02:28
This was very helpful , than you so much.
Quote
#3Bibha Kumari2018-07-28 04:43
Thanks for articlesQuoting Bibha Kumari:
Rectangle rect = new Rectangle();
ShapeUtils.draw(rect);
Above output is not correct it should be
draws a general shape
Quoting Bibha Kumari:
Rectangle rect = new Rectangle();
ShapeUtils.draw(rect);
Above output is not correct it should be
draws a general shape



Sorry, I have missed some part of the code. Your output is correct.
Quote
#2Bibha Kumari2018-07-28 04:35
Rectangle rect = new Rectangle();
ShapeUtils.draw(rect);
Above output is not correct it should be
draws a general shape
Quote