We may not have the course you’re looking for. If you enquire or give us a call on +08000201623 and speak to our training experts, we may still be able to help with your training requirements.
Training Outcomes Within Your Budget!
We ensure quality, budget-alignment, and timely delivery by our expert instructors.
Method Overloading in Java is an important feature that simplifies the work of Java users. The credibility of Java as a programming language is sustained due to its list of convenient features like Method Overloading, ease of usage, and a massive library.
Java is perhaps among the most used programming languages today, it can run on a plethora of devices for a gazillion purposes. Thus, improving your knowledge of the features and formulas in Java is a great way to gain a better understanding and to get better at it. Method Overloading in Java is when a class contains multiple methods with the same name but different argument lists. Let’s dive in to learn more.
Table of Contents
1) What is Method Overloading in Java?
2) Method Overloading: Benefits
3) How do you initiate Method Overloading in Java?
a) Change in the number of parameters
b) Change in the data types of arguments
4) Conclusion
What is Method Overloading in Java?
Java allows the creation of multiple methods in a class, which can have the same names. But all the methods will be assigned with different parameters. This feature is known as Java Method Overloading. The primary significance of this feature is that it increases the readability of programs. Method Overloading is often known as Compile-time polymorphism.
Let’s explore the Method Overloading feature through an example. For instance, we define two methods using the same name – add – but different parameters. Having the same name in the same class helps increase the program's readability. The Java code used in this example would be as follows:
public class Example
{
public static void main(String[] args)
{
Example obj = new Example();
System.out.println(obj.add(10, 20));
System.out.println(obj.add(10, 20, 30));
}
public int add(int a, int b)
{
return a + b;
}
public int add(int a, int b, int c)
{
return a + b + c;
}
}
The first method, ' add(int a, int b),' takes two integer-based parameters and returns their sum, while the second method, 'add(int a, int b, int c),' takes three integer parameters and returns their sum.
Output:
30
Method overloading: Benefits
The definition of Method Overloading is highly similar to that of Constructor Overloading in Java, but they're not the same. The former is a fairly popular feature in Java, with multiple advantages. The following are some benefits of using Method Overloading:
a) Method overloading significantly improves the readability of programs.
b) The feature allows programmers to operate on tasks efficiently.
c) Using Method Overloading results in codes that look neat and compact.
d) Complex programs can be simplified using Method Overloading.
e) Codes can be reused using this feature, which helps save memory as well.
f) Since binding is accomplished in the compilation time, the execution period shortens.
g) Method Overloading can also be used on constructors, which offers you opportunities to initialise objects of a class in different ways.
h) Using Method Overloading, you can access the methods that perform related functions that use the same name, with a minor difference in the argument number or type.
How do you initiate method overloading in Java?
Method overloading can be used by changing the number of parameters or the data type. The best way to understand concepts in Java is via examples. Here is a detailed look into the different ways to initiate Method Overloading in Java with examples:
Change in the number of parameters
One of the ways to accomplish Method Overloading is by changing the number of parameters. This is done while passing to different methods. Here is an example of changing the number of parameters while using Method Overloading in Java:
public class Calculator
{
// Method to add two integers
public int add(int a, int b)
{
return a + b;
}
// Overloaded method to add three integers
public int add(int a, int b, int c)
{
return a + b + c; }
// Overloaded method to add two doubles
public double add(double a, double b)
{
return a + b;
}
// Overloaded method to add three doubles
public double add(double a, double b, double c)
{
return a + b + c;
}
}
Here, a 'Calculator' class with four methods named 'add' is taken. The first method takes two 'int' parameters and returns their sum as an 'int'. The second method is an overloaded version of the first but takes three 'int' parameters instead of two.
The third method takes two 'double' parameters and returns their sum as a 'double'. The fourth method is an overloaded version of the third but takes three 'double' parameters instead of two. You can use the 'add' method to add two integers, three integers, two doubles, or three doubles, depending on the arguments we pass to the method:
public static void main(String[] args)
{
Calculator calculator = new Calculator();
// Adding two integers
int sum1 = calculator.add(5, 10);
System.out.println(sum1); // Output: 15
// Adding three integers
int sum2 = calculator.add(5, 10, 15);
System.out.println(sum2); // Output: 30
// Adding two doubles
double sum3 = calculator.add(5.5, 10.5);
System.out.println(sum3); // Output: 16.0
// Adding three doubles
double sum4 = calculator.add(5.5, 10.5, 15.5);
System.out.println(sum4); // Output: 31.5
}
Output:
15
30
16.0
31.5
Expand your skillset by learning to enhance web pages with dynamic effects in JavaScript. Sign up for our JavaScript & jQuery course now!
Change in the data types of arguments
Method Overloading can also be accomplished by having different parameter types with the same name. Here is an example of method overloading in Java where multiple methods differ in data type:
public class Example
{
public int sum(int a, int b)
{
return a + b;
} public double sum(double a, double b)
{
return a + b;
} public String sum(String a, String b)
{
return a + b;
} public static void main(String[] args)
{
Example ex = new Example();
int result1 = ex.sum(2, 3);
double result2 = ex.sum(2.5, 3.5);
String result3 = ex.sum("Hello ", "World");
System.out.println("Result1: " + result1);
System.out.println("Result2: " + result2);
System.out.println("Result3: " + result3);
}
}
In this example, three methods are mentioned with the same name, "sum", but with different data types and output types. The first method takes two integers as input and returns an integer.
The second method takes two doubles as input and returns a double, and the third method uses two strings as input and returns a string. Then an object of the Example class is created in the main method.
The sum method with different arguments is called, and the variables with varying types of data are assigned with the results. Finally, the results are printed using 'System.out. println' statements.
Output:
Result1: 5
Result2: 6.0
Result3: Hello World
From the output we understand the following:
a) The first call to the sum method with 2 and 3 as arguments return 5
b) The second call to the sum method with 2.5 and 3.5 as arguments return 6.0
c) The third call to the sum method with "Hello " and "World" as arguments concatenates the two strings and returns "Hello World"
d) The System.out.println statements then print out the values of the result1, result2, and result3 variables, respectively.
Stand out among your peers by learning to test your JavaScript applications the right way. Enroll for our JavaScript Unit Testing Training with Jasmine course now!
The concept of overloading static methods in Java carries a lot of doubt among users because they confuse the term 'overloading' with 'overriding'. The overloading of static methods is allowed in Java while overriding isn't.
Overloading and overriding are different in a couple of aspects. While overloading has a different number of parameters, overriding has the same. Another difference between the two is regarding the number of methods involved. Overloading requires at least two methods by the same name in a class while overriding needs at least one method by the same name in both the parent and child classes.
Overriding static methods is permitted due to its reliance on dynamic binding at runtime, while bonding of static methods occurs at compile time with static binding. The process is also known as run time polymorphism.
The main() method in Java can be overloaded. The significance of the main() method is that it is considered the starting point for the processing of Java programs. The syntax of the method is the public static void main (String[] args). The syntax never changes, although the String array argument name can be changed.
Conclusion
Method Overloading in Java is a useful feature that makes Java programming easier and more efficient. We hope this blog has helped you understand the concept of Method Overloading, the different ways to perform it, its benefits, and more.
Understand the key concepts of JavaScript by signing up for our JavaScript for Beginners course now!
Frequently Asked Questions
Operator overloading is the ability to redefine the functionality of the operators for user-defined types. For example, you can use the + operator to add two complex numbers or two matrices. However, Java does not support user-defined operator overloading. You cannot create your own overloaded operators in Java.
The only aspect of Java which comes close to “custom” operator overloading is the handling of + for strings, which either results in compile-time concatenation of constants or execution-time concatenation using StringBuilder/StringBuffer. This is a design choice made by the Java developers to keep the language simple and avoid confusion.
No, we cannot overload methods that differ only by static keywords in Java. If we try to do so, we will get a compiler error, as the compiler cannot distinguish between the methods based on the static keyword alone.
The static keyword indicates whether the method belongs to the class or the instance, but it does not affect the method signature, which is used for overloading. Therefore, we need to have some other difference in the method name, parameters, or return type to overload methods in Java.
The Knowledge Academy takes global learning to new heights, offering over 30,000 online courses across 490+ locations in 220 countries. This expansive reach ensures accessibility and convenience for learners worldwide.
Alongside our diverse Online Course Catalogue, encompassing 17 major categories, we go the extra mile by providing a plethora of free educational Online Resources like News updates, Blogs, videos, webinars, and interview questions. Tailoring learning experiences further, professionals can maximise value with customisable Course Bundles of TKA.
The Knowledge Academy’s Knowledge Pass, a prepaid voucher, adds another layer of flexibility, allowing course bookings over a 12-month period. Join us on a journey where education knows no bounds.
The Knowledge Academy offers various Java Courses, including Java Programming Course, JavaScript for Beginners and Java Engineer Training. These courses cater to different skill levels, providing comprehensive insights into Latest Java Technologies Trends.
Our Programming & DevOps blogs cover a range of topics related to Java Programming Course, offering valuable resources, best practices, and industry insights. Whether you are a beginner or looking to advance your Programming skills, The Knowledge Academy's diverse courses and informative blogs have you covered.
Upcoming Programming & DevOps Resources Batches & Dates
Date
Mon 13th Jan 2025
Mon 10th Mar 2025
Mon 19th May 2025
Mon 21st Jul 2025
Mon 15th Sep 2025
Mon 17th Nov 2025
Mon 15th Dec 2025