Technique overloading in java is a function that enables a category to have multiple methodology with the identical title, however with completely different parameters.
Java helps methodology overloading by means of two mechanisms:
- By altering the variety of parameters
- By altering the information sort of parameters Overloading by altering the variety of parameters A way may be overloaded by altering the variety of parameters.
What’s Technique overloading in Java?
“Technique overloading is a function of Java through which a category has multiple methodology of the identical title and their parameters are completely different.”
In different phrases, we will say that Technique overloading is an idea of Java through which we will create a number of strategies of the identical title in the identical class, and all strategies work in several methods. When multiple methodology of the identical title is created in a Class, this kind of methodology is known as the Overloaded Technique.
We will simply perceive about methodology of overloading by the beneath instance:
Suppose we’ve got to write down a technique to search out the sq. of an integer quantity. We will write this methodology as follows:
public void intSquare ( int quantity )
{
int sq. = quantity * quantity;
System.out.printIn("Technique with Integer Argument Known as:"+sq.);
}
Suppose we need to discover the sq. of 10, then to search out the sq. of 10, we will name this methodology as follows:
intSquare(5);
Now, if we need to discover the Sq. of a double sort worth, then we’ve got to create one other Sq. () methodology as follows:
public void doubleSquare(double quantity)
{
double sq. = quantity * quantity;
System.out.printIn("Technique with double Argument Known as:"+sq.);
}
Equally, if we need to discover the sq. of lengthy sort worth, then we’ve got to create one other methodology as follows:
public void longSquare(lengthy quantity)
{
lengthy sq. = quantity * quantity;
System.out.printIn("Technique with lengthy Argument Known as:"+sq.);
}
If we glance fastidiously, to search out the sq. of a quantity solely, in keeping with the information sort of the quantity, we’ve got to take three names as follows:
intSquare()
doubleSquare()
longSquare()
Whether it is attainable {that a} programmer has to take just one title and this system itself decides which methodology to make use of for which kind of worth, then it is going to be simpler for the programmer to get the identical. There isn’t any have to memorise the names of multiple methodology for sort work. In Java, we may give the above three strategies the identical title.
If we offer solely the sq. () title as a substitute of giving completely different names to the above three strategies and write the remainder of the outline as follows, then Java’s Compiler doesn’t generate any error.
public void Sq. ( int quantity )
{
int sq. = quantity * quantity;
System.out.printIn(“Technique with Integer Argument Known as:“+sq.);
}
public void Sq.(double quantity)
{
double sq. = quantity * quantity;
System.out.printIn(“Technique with double Argument Known as:“+sq.);
}
public void Sq.(lengthy quantity)
{
lengthy sq. = quantity * quantity;
System.out.printIn(“Technique with lengthy Argument Known as:“+sq.);
}
If we outline these three strategies in a category, then these strategies may be referred to as Overloaded Strategies as they’ve the identical title. Allow us to develop CalculateSquare Class to know this, which is as follows:
class CalculateSquare
{
public void sq.()
{
System.out.println("No Parameter Technique Known as");
}
public int sq.( int quantity )
{
int sq. = quantity * quantity;
System.out.println("Technique with Integer Argument Known as:"+sq.);
}
public float sq.( float quantity )
{
float sq. = quantity * quantity;
System.out.println("Technique with float Argument Known as:"+sq.);
}
public static void principal(String[] args)
{
CalculateSquare obj = new CalculateSquare();
obj.sq.();
obj.sq.(5);
obj.sq.(2.5);
}
}
Observe: We’ve got not supplied any argument within the ‘parenthesis’ of the sq.() methodology in our program. On this case, the Compiler Class calls the strategy through which no Parameter has been outlined to attain the Argument.
Output:
No Parameter Technique Known as
Technique with Integer Argument Known as: 25
Technique with float Argument Known as: 6.25
On this approach, we will outline multiple Strategies of the identical title in a category, which is known as Technique Overloading, and
The Java compiler itself performs the suitable Technique Name for an object, based mostly on the Information Kind of the Arguments of the Strategies.
Advantages of utilizing Technique Overloading
- Technique overloading will increase the readability of this system.
- This gives flexibility to programmers in order that they will name the identical methodology for several types of information.
- This makes the code look clear.
- This reduces the execution time as a result of the binding is completed in compilation time itself.
- Technique overloading minimises the complexity of the code.
- With this, we will use the code once more, which saves reminiscence.
Methods to do Technique Overloading?
In java, we do methodology overloading in two methods:
- By altering the variety of parameters.
- By altering information sorts.
- Change the variety of arguments:
Within the instance beneath, we’ve got two strategies, the primary methodology has two arguments, and the second methodology has three arguments.
class Demo
{
void multiply(int a, int b)
{
System.out.printIn("Result's"+(a*b)) ;
}
void multiply(int a, int b,int c)
{
System.out.printIn("Result's"+(a*b*c));
}
public static void principal(String[] args)
{
Demo obj = new Demo();
obj.multiply(8,5);
obj.multiply(4,6,2);
}
}
Output:-
Result’s 40
Result’s 48.
- By Altering the information sorts:
Within the instance given beneath, we’ve got two strategies, and their information sorts are completely different.
class Sum
{
static int add(int a, int b)
{
return a+b;
}
static double add(double a, double b)
{
return a+b;
}
}
class TestOverloading2
{
public static void principal(String[] args)
{
System.out.println(Sum.add(17,13));
System.out.println(Sum.add(10.4,10.6));
}
}
Observe: On this instance, we’re creating static strategies in order that we don’t have to create an occasion for calling strategies.
Output:
30, 21
Some factors to recollect about methodology overloading:
- Technique overloading can’t be performed by altering the return sort of strategies.
- An important rule of methodology overloading is that two overloaded strategies should have completely different parameters.
Technique overloading has nothing to do with return-type.
If there are two strategies of the identical signature inside a category in this system, then Ambiguity Error comes, whether or not their return-type is completely different or not. Because of this methodology overloading has no relation with return-type.
class Pattern{
int disp(int x){
return x;
}
double disp(int y){
return y;
}
public static void principal(String args[])
{
Pattern s = new Pattern();
System.out.printIn("Worth of x : " + s.disp(5));
System.out.printIn("Worth of y : " + s.disp(6.5));
}
}
Output:
Pattern.java:6: error: methodology disp(int) is already outlined in school Pattern
On this approach, we will outline multiple Strategies of the identical title in a category referred to as Technique Overloading. The Java Compiler itself, based mostly on the Information Kind of the Arguments of the Strategies, performs the suitable methodology name for an object.
This brings us to the top of the weblog on Technique Overloading in Java. Hope this lets you up-skill your C++ abilities. To study extra about programming and different associated ideas, take a look at the programs on Nice Studying Academy.
Additionally, if you’re making ready for Interviews, take a look at these Interview Questions for Java to ace it like a professional.
Additionally Learn: Methods to make Resume for Java Developer