Core java tutorial for beginners
A tutorial blog which explains different core concepts related to Java along with programming examples
Subscribe to Startertutorials.com's YouTube channel for different tutorial and lecture videos.

Categories: Core Java Basics. No Comments on Java Overloading
4
(3)

In this article you are going to learn about overloading in Java. You will look at what is overloading? why should we use overloading? along with details and examples on method overloading and constructor overloading.

 

Introduction

 


Subscribe to our monthly newsletter. Get notified about latest articles, offers and contests.


One of the way through which Java supports polymorphism is overloading. It can be defined as creating two or more methods in the same class sharing a common name but different number of parameters or different types of parameters.

 

You should remember that overloading doesn’t depend upon the return type of the method. Since method binding is resolved at compile-time based on the number of parameters or type of parameters, overloading is also called as compile-time polymorphism or static binding or early binding.

 

Why overloading?

 

In Java, overloading provides the ability to define two or more methods with the same name. What is the use of that? For example, you want to define two methods, in which, one method adds two integers and the second method adds two floating point numbers.

 

If there is no overloading, we have to create two different methods. One for adding two integers and the other for adding two floating point numbers. As the underlying purpose of the methods is same, why create methods with different name? Instead of creating two different methods, overloading allows us to define two methods with the same name.

 

Method Overloading

 

Creating two or more methods in the same class with same name but different number of parameters or different types of parameters is known as method overloading. Let’s consider the following code segment which demonstrates method overloading:

class Addition
{
	void sum(int a, int b)
	{
		System.out.println("Sum of two integers is: "+(a+b));
	}
	void sum(float a, float b)
	{
		System.out.println("Sum of two floats is: "+(a+b));
	}
}

 

In the above code segment, the method sum is overloaded. Java compiler decides which method to call based on the type of the parameters in the method call. For example, if the method is called as shown below:

Addition obj = new Addition();
obj.sum(10, 20);

 

The sum method with two integer parameters will be invoked and the output will be Sum of two integers is: 30. 

 

If the method is called as shown below:

Addition obj = new Addition();
obj.sum(1.5, 1.2);

 

The sum method with two float parameters will be invoked and the output will be Sum of two floats is: 2.7.

 

Note: It should be remembered that Java automatically performs type conversion from one type to another type. So, proper care should be taken while defining overloaded methods.

 

Constructor Overloading

 

As constructor is a special type of method, constructor can also be overloaded. Several constructors can be defined in the same class given that the parameters vary in each constructor. As an example for constructor overloading, let’s consider the following code segment:

class Square
{
	int side;
	Square(int s)
	{
		side = s;
		//Code to create a square and return it
	}
	Sqaure(int s, int n)
	{
		side = s;
		//Code to create n number of squares
	}
}

 

In the above code segment we can see that the constructor Sqaure() is overloaded. One constructor accepts a single integer parameter and returns a single square with side s. Another constructor accepts two integer parameters and returns n number of squares each with side s.

 

Note: In the above examples I have defined only two overloaded methods or constructors. You can create any number of overloaded methods or constructors based on your requirements.

How useful was this post?

Click on a star to rate it!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Suryateja Pericherla

Suryateja Pericherla, at present is a Research Scholar (full-time Ph.D.) in the Dept. of Computer Science & Systems Engineering at Andhra University, Visakhapatnam. Previously worked as an Associate Professor in the Dept. of CSE at Vishnu Institute of Technology, India.

He has 11+ years of teaching experience and is an individual researcher whose research interests are Cloud Computing, Internet of Things, Computer Security, Network Security and Blockchain.

He is a member of professional societies like IEEE, ACM, CSI and ISCA. He published several research papers which are indexed by SCIE, WoS, Scopus, Springer and others.

Note: Do you have a question on this article or have a suggestion to make this article better? You can ask or suggest us by filling in the below form. After commenting, your comment will be held for moderation and will be published in 24-48 hrs.

Leave a Reply

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