METHODOVERLOADING IN JAVA
WHAT IS METHODOVERLOADING 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.
If a class has multiple methods having same name but different in parameters, it is known as Method Overloading.If we have to perform only one operation, having same name of the methods increases the readability of the program.Suppose you have to perform addition of the given numbers but there can be any number of arguments, if you write the method such as a(int,int) for two parameters, and b(int,int,int) for three parameters then it may be difficult for you as well as other programmers to understand the behavior of the method because its name differs.So, we perform method overloading to figure out the program quickly.
// Java program to demonstrate working of method
// overloading in Java
public class Sum {
// Overloaded sum(). This sum takes two int parameters
public int sum(int x, int y) { return (x + y); }
// Overloaded sum(). This sum takes three int parameters
public int sum(int x, int y, int z)
{
return (x + y + z);
}
// Overloaded sum(). This sum takes two double
// parameters
public double sum(double x, double y)
{
return (x + y);
}
// Driver code
public static void main(String args[])
{
Sum s = new Sum();
System.out.println(s.sum(10, 20));
System.out.println(s.sum(10, 20, 30));
System.out.println(s.sum(10.5, 20.5));
}
}
=====OUTPUT======
30 60 31.0
Advantage of method overloading::
Method overloading increases the readability of the program
Different ways to overload the method::
There are two ways to overload the method in java::
- By changing number of argument
- By changing the data type of argument
1) Method Overloading: changing no. of arguments
In this example, we have created two methods, first add() method performs addition of two numbers and second add method performs addition of three numbers.
In this example, we are creating static method
so that we don't need to create instance for calling methods.
// Java Program to Illustrate Method Overloading
// By Changing the Number of Arguements
// Importing required classes
import java.io.*;
// Class 1
// Helper class
class Product {
// Method 1
// Multiplying two integer values
public int multiply(int a, int b)
{
int prod = a * b;
return prod;
}
// Method 2
// Multiplying three integer values
public int multiply(int a, int b, int c)
{
int prod = a * b * c;
return prod;
}
}
// Class 2
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating object of above class inside main()
// method
Product ob = new Product();
// Calling method to Multiply 2 numbers
int prod1 = ob.multiply(1, 2);
// Printing Product of 2 numbers
System.out.println(
"Product of the two integer value :" + prod1);
// Calling method to multiply 3 numbers
int prod2 = ob.multiply(1, 2, 3);
// Printing product of 3 numbers
System.out.println(
"Product of the three integer value :" + prod2);
}
}
Output
Product of the two integer value :2 Product of the three integer value :6
2) Method Overloading: changing data type of arguments
In this example, we have created two methods that differs in data type. The first add method receives two integer arguments and second add method receives two double arguments.
// Java Program to Illustrate Method Overloading
// By Changing Data Types of the Parameters
// Importing required classes
import java.io.*;
// Class 1
// Helper class
class Product {
// Multiplying three integer values
public int Prod(int a, int b, int c)
{
int prod1 = a * b * c;
return prod1;
}
// Multiplying three double values.
public double Prod(double a, double b, double c)
{
double prod2 = a * b * c;
return prod2;
}
}
class GFG {
public static void main(String[] args)
{
Product obj = new Product();
int prod1 = obj.Prod(1, 2, 3);
System.out.println(
"Product of the three integer value :" + prod1);
double prod2 = obj.Prod(1.0, 2.0, 3.0);
System.out.println(
"Product of the three double value :" + prod2);
}
}
Output
Product of the three integer value :6 Product of the three double value :6.0
Method Overloading and Type Promotion
One type is promoted to another implicitly if no matching datatype is found. Let's understand the concept by the figure given below:
As displayed in the above diagram, byte can be promoted to short, int, long, float or double. The short datatype can be promoted to int, long, float or double. The char datatype can be promoted to int,long,float or double and so on.
Comments
Post a Comment