Method Overloading in Java

Method Overloading in Java

5 mins read18.7K Views Comment
clickHere
Updated on Dec 29, 2023 16:49 IST

This tutorial article covers Method overloading in Java and ways to implement it under different scenarios. Moreover, it goes through several questions that arise during overloading.

2022_06_Copy-of-Copy-of-Feature-Image-Templates-3.jpg

Java provides a powerful feature called “method overloading, ” which allows programmers to define multiple methods with the same name but different parameters. This makes the code more readable and easier to understand and provides flexibility and convenience when working with different data types or combinations of inputs. Whether a beginner or an experienced Java developer, understanding method overloading can greatly enhance your programming skills and help you write more efficient and effective code. Let’s learn more about this concept!

Explore Free Java Courses

Contents

Explore Online Java Courses

Also Read: Method Overriding in Java

What is Method Overloading in Java?

Method overloading in Java means having two or more methods (or functions) in a class with the same name and different arguments (or parameters). It can be with a different number of arguments or different data types of arguments. 

Also Read: Data Types in Java – Primitive and Non-Primitive Data Types Explained

For instance:

void function1(double a) { ... }
void function1(int a, int b, double c) { ... }
float function1(float a) { ...}
double function1(int a, float b) { ... }

In the above example, function1() is overloaded using a different number of parameters and different data types of parameters.

Unlock the secrets of Java with our career prospects and comprehensive guide; discover information on top collegesspecialised programmes and online courses to excel in the world of Java!

Also Read: Variables in Java

Example:


 
class Method_Overloading {
double figure(double l, int b) //two parameters with double type
{
return (l*b);
}
float figure(int s) //one parameter with float return type
{
return (s*s);
}
public static void main(String[] args) {
Method_Overloading obj = new Method_Overloading();
System.out.println("Area of Rectangle: " +obj.figure(5.55, 6));
System.out.println("Area of Square: " +obj.figure(3));
}
}
Copy code

However, when we change the return type, we have to consider automatic type promotion. For example, passing double as a parameter and returning it doesn’t make sense. The reverse is possible as int takes 2 bytes, but double can adjust up to 8 bytes.

Output:

2022_06_image-92.jpg

Read: Access Modifiers in Java

Also Read: Difference Between Overloading and Overriding in Java

Let’s understand why do we overload a method?

Java Methods Explained (with Code)
Java Methods Explained (with Code)
The below java tutorial covers java methods with examples and explanations. It also goes through the declaration, implementation, types, and its uses.
Understanding ArrayList in Java
Understanding ArrayList in Java
The below article goes through explaining ArrayList in Java with suitable examples. It covers the creation and operations on ArrayList along with a few methods in it. Let’s begin!
Multithreading in Java
Multithreading in Java
The below article goes through explaining the concepts of Multithreading in Java and its implementation. Let’s begin the tutorial.

Why Method Overloading?

We have several methods with the same working but different parameters/arguments. 

Suppose, in a software development company, several developers are categorized under junior developers, senior developers, expert developers, etc. However, they work similarly and generate similar output. Here, we can use function overloading. Instead of creating various methods with different method names. We can overload a single function name developer here. 

Explore: Constructors in Java

How to Implement Method Overloading in Java

Following are the ways to implement Method Overloading in Java:

By changing the Number of Parameters

A method can differ with the number of parameters passed. The below example shows how we can implement method overloading with the different number of parameters in method declaration and definition.

Example:


 
class Method_Overloading {
//Method Overloading by changing the number of arguments (or parameters)
//Method 1
double figure(double l, double b) //two arguments or parameters
{
return (l*b);
}
double figure(double s) //one argument or parameter
{
return (s*s);
}
//Method 2
public static void main(String[] args) {
Method_Overloading obj = new Method_Overloading();
System.out.println("Area of Rectangle: " +obj.figure(5.55, 6.78));
System.out.println("Area of Square: " +obj.figure(3.45));
}
}
Copy code

Also, Read: Java Operators Explained

Output:

output2

Read: Exception Handling in Java.

 

Changing the Data Type of Parameters

Another way to do method overloading is by changing the data types of method parameters. The below example shows how we can implement method overloading with the different data types of parameters in method declaration and definition.

Example:


 
class Method_Overloading {
//Method Overloading by changing the data type of arguments (or parameters)
double figure(double l, double b) //method 1
{
return (l*b); //returns area of rectangle
}
double figure(int b, int h) //method 2
{
return ((b*h)/2); //returns area of right triangle
}
double figure(int b, double h) //method 3
{
return (b*h); //returns area of parallelogram
}
public static void main(String[] args) {
Method_Overloading obj = new Method_Overloading();
System.out.println("Area of Rectangle: " +obj.figure(5.55, 6.78));
System.out.println("Area of Right Triangle: " +obj.figure(3,5));
System.out.println("Area of Parallelogram: " +obj.figure(4,6.3));
}
}
Copy code

Output:

output3

Read: Implementing Array in Java

Can we overload Static Methods?

Yes, static methods can also be overloaded. The below example shows a static method pet overloaded with different types and number of parameters.

Example:


 
// Java program to show the working of static method overloading
class Static_Method_Overloading
{
static String pet(String s1)
{
return ("I have a cat named: " +s1);
}
static String pet(String n1, String n2)
{
return ("I have two dogs named: " +n1+" and "+n2);
}
static int pet (int num)
{
return num+3;
}
public static void main(String args[])
{
System.out.println(pet("Ms. Whiskerson"));
System.out.println(pet("Scooby", "Frodo"));
System.out.println("Total number of Pets: " +pet(2));
}
}
Copy code

Output:

output4

Also, Read: Final Keyword in Java

Can we overload main() in Java?

Yes, main() method can also be overloaded. The below example shows a simple implementation to overload the main() method with a different set of parameters.

Example:


 
class Main_Overloading {
//main 1
public static void main(String[] args)
{
System.out.println("Overloading from main 1");
Main_Overloading.main("User!");
}
//main 2
public static void main(String arg1)
{
System.out.println("Hello, " + arg1 +" Overloading main 2");
Main_Overloading.main("Are you learning", " Main Method overloading!");
}
//main 3
public static void main(String arg1, String arg2)
{
System.out.println("Overloading main 3");
System.out.println("Hi, " + arg1 + ", " + arg2);
}
}
Copy code

Output:

output5

Conclusion 

The article explained the Java Overloading method using various scenarios and examples. I hope you learned something new by reading this tutorial. Stay tuned for more articles on Java, and share your queries at the link below.

8 Most Important Data Structures Every Programmer Must Know
8 Most Important Data Structures Every Programmer Must Know
Knowledge of data structures and algorithms defines the programmers and their coding skills. Hence, it inevitably becomes essential to learn and implement the data structure for a software engineer. The...read more
Understanding OOPs Concepts in Java
Understanding OOPs Concepts in Java
Object oriented programming (OOP) concepts are the fundamental pillars of programming. The article below explains the OOPs concept in Java. It covers introductions to OOPs, class, objects, inheritance, abstraction, encapsulation,...read more
Polymorphism in Java: Meaning and its Types
Polymorphism in Java: Meaning and its Types
Polymorphism is a fancy-sounding word that really just means “many shapes.” And in the world of Java programming, it refers to the ability of objects to take on different forms...read more

FAQs

What is method overloading in Java?

Method overloading is a feature in Java that allows a programmer to define multiple methods in a class with the same name, but with different parameters.

What is the difference between Overloading and Overriding?

Method Overloading has two or more methods in a class with the same name but different parameters. Whereas Method Overriding is redefining a superclass method with the same name, type, and parameters in a subclass.

Can we overload methods on return type?

In Method Overloading changing the return type is possible only with logical implementation and type promotion. As in, you cannot multiple 2 double type variables and return an int type. So, these are the things you have to be clear about it.

Why method overriding is used?

Method overriding is used in object-oriented programming when a subclass provides its own implementation of a method that is already defined in its superclass. This is done to customize the behavior of the method for the subclass, which may have specific requirements or functionality that differ from the superclass.

What is the difference between Polymorphism and Overriding?

Polymorphism and overriding are related concepts in object-oriented programming, but they have different meanings and purposes. Polymorphism refers to the ability of objects of different classes to be treated as if they are of the same type. This is achieved through inheritance and interfaces, which allow objects to be grouped together based on their common behaviour or functionality. Polymorphism allows for greater flexibility and extensibility in the design of programs, as it enables code to be written that can work with a variety of different objects, without needing to know their specific types.

What is method overloading and its types?

Method overloading is a technique used in object-oriented programming where multiple methods can have the same name but different parameters. Method overriding is used in inheritance, where a subclass provides its own implementation of a method that is already defined in its superclasses. Method overloading is used to define multiple methods with the same name but different parameters in a class.

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio

Comments