Table of Contents
Working of Function Overloading
Advantage of Function Overloading
Examples of Function Overloading
Example #3
Conclusion
Home Java javaTutorial Function Overloading in Java

Function Overloading in Java

Aug 30, 2024 pm 03:44 PM
java

Function Overloading in Java occurs when there are functions having the same name but have different numbers of parameters passed to it, which can be different in data like int, double, float and used to return different values are computed inside the respective overloaded method. Function overloading is used to reduce complexity and increase the efficiency of the program by involving more functions that are segregated and can be used to distinguish among each other with respect to their individual functionality. Overloaded functions are related to compile-time or static polymorphism. There is also a concept of type conversion, which is basically used in overloaded functions used to calculate the conversion of type in variables.

Syntax

ADVERTISEMENT Popular Course in this category JAVA MASTERY - Specialization | 78 Course Series | 15 Mock Tests

Overloaded functions have the same name but different types of arguments or parameters assigned to them. They can be used to calculate mathematical or logical operations within the number of assigned variables in the method. The syntax of the overloaded function can be given below, where there are up to N number of variables assigned.

public class OverloadedMethod
{
public int FunctionName(int x, int y) //Two parameters in the function
{
return (x + y); //Returns the sum of the two numbers
}
// This function takes three integer parameters
public int FunctionName(int x, int y, int z)
{
return (x + y + z);
}
// This function takes two double parameters
public double FunctionName(double x, double y)
{
return (x + y);
}//Many more such methods can be done with different number of parameters
// Code used to input the number and
public static void main(String args[])
{
FunctionName s = new FunctionName();
System.out.println(s.FunctionName(10, 20));
System.out.println(s. FunctionName(10, 20, 30));
System.out.println(s. FunctionName(10.5, 20.5));
}
}
Copy after login

Working of Function Overloading

Function overloading works by calling different functions having the same name, but the different number of arguments passed to it. There are many coding examples that can be shown in order to identify the benefits and disadvantages of function overloading properly.

Advantage of Function Overloading

Function overloading works with the same name. So we do not have to create methods that have the same thing as work that is done inside a respective function. The functionality not only resolves the problem of conflicting naming but also improves the readability of the program.

Examples of Function Overloading

The following examples are as given below:

Example #1

In coding example 1, we will see overloaded main(), which is used to show different outputs in the panel and show how the overloaded main() is used in programming languages and how the calling of the different functions produces different outputs, respectively.

Code:

//Java program with overloaded main()
import java.io.*;
public class Test
{
// First main method which is created
public static void main(String[] args)
{
System.out.println("Main function called");
Test.main("Everyone");
}
// First overloaded main method
public static void main(String ar1)
{
System.out.println("Hi, " + ar1);
Test.main("Hello Everyone", "Buenos dias");
}
//Second overloaded main method
public static void main(String ar1, String ar2)
{
System.out.println("Mi todo eres tu, " + ar1 + ", " + ar2);
}
}
Copy after login

When the main() is called, there is output which is shown significantly in the output panel as shown below. First, the print statement of the main() is called and printed. Then, by distinguishing the number of arguments, the program separates the two overloaded functions which are present in the piece of code. The main() calls the first overloaded function, while the first overloaded function calls the second overloaded function.

Output:

Function Overloading in Java

Example #2

In the second coding example, we are going to see functions that will perform multiplication but which will have a different number of arguments assigned to them. This will help people in developing the different functions which are meant for dealing with different arguments.

Code:

class Adder
{
static int multiply(int a,int b)
{
return a*b;
}
static int multiply(int a,int b,int c)
{
return a*b*c;}
}
class TestOverloading1
{
public static void main(String[] args)
{
System.out.println(Adder.multiply(110,110));
System.out.println(Adder.multiply(110,110,110));
}
}
Copy after login

Output:

Function Overloading in Java

Example #3

In this coding example, we are going to see functions that have the same name and the same number of arguments inside them, yet they have one functionality that serves as a difference between them. The difference in functionality that is present is the datatype which is present in the functions, one is an integer, and the other is double.

Code:

//Program to add numbers distinguishing on the datatype of parameters
class Number
{
static int add(int a, int b)//Both integer variables
{
return a+b;//Returning the sum
}
static double add(double a, double b)//Both double variables
{
return a+b;//Returning the sum
}
}
class TestOverloading2
{
public static void main(String[] args)
{
System.out.println(Number.add(1,110));
System.out.println(Number.add(12.4,18.8));
}
}
Copy after login

In the above program, we input two integer values when we compute the sum of two integer values. The numbers entered are 10  and 20. The output should give us the value 30, which would print the sum as it is. Also, when we enter two double values, the sum is printed, which is the second overloaded function. We enter the values of the double data type variables as 15.5 and 16.3, and then we finally get to see the result as 31.8, which is the sum. We will see the output panel of the program in the below screen.

Output:

Function Overloading in Java

Conclusion

In this article, we see the different functionalities and concepts of Overloaded functions, which have the same name, but the different number of arguments or the data-types are different. We also notice the advantages and the syntax as to how overloaded functions need to be carried out. In this article, the overloaded functions are mainly used to multiply and do addition, which gives a great deal of detailing to the entire programming concept.

The above is the detailed content of Function Overloading in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1669
14
PHP Tutorial
1273
29
C# Tutorial
1256
24
PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP vs. Python: Core Features and Functionality PHP vs. Python: Core Features and Functionality Apr 13, 2025 am 12:16 AM

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHP's Impact: Web Development and Beyond PHP's Impact: Web Development and Beyond Apr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP: The Foundation of Many Websites PHP: The Foundation of Many Websites Apr 13, 2025 am 12:07 AM

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

PHP vs. Python: Use Cases and Applications PHP vs. Python: Use Cases and Applications Apr 17, 2025 am 12:23 AM

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.

See all articles