Sunday, November 27, 2022
HomeArtificial IntelligenceMethodology Overloading in Java with Examples

Methodology Overloading in Java with Examples


Method Overloading in Java

Methodology overloading in java is a characteristic that permits a category to have multiple methodology with the identical identify, however with totally different parameters.

Java helps methodology overloading by means of two mechanisms:

  1. By altering the variety of parameters
  2. By altering the information kind of parameters Overloading by altering the variety of parameters A way might be overloaded by altering the variety of parameters.

What’s Methodology overloading in Java?

“Methodology overloading is a characteristic of Java through which a category has multiple methodology of the identical identify and their parameters are totally different.”

In different phrases, we will say that Methodology overloading is an idea of Java through which we will create a number of strategies of the identical identify in the identical class, and all strategies work in several methods. When multiple methodology of the identical identify is created in a Class, such a methodology is known as the Overloaded Methodology.

We are able to simply perceive about methodology of overloading by the beneath instance:

Suppose we’ve to put in writing a technique to search out the sq. of an integer quantity. We are able to write this methodology as follows:

public void intSquare ( int quantity ) 
{
 int sq. = quantity * quantity;
 System.out.printIn("Methodology 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 kind worth, then we’ve to create one other Sq. () methodology as follows:

public void doubleSquare(double quantity)
{
 double sq. = quantity * quantity; 
 System.out.printIn("Methodology with double Argument Known as:"+sq.);
}

Equally, if we need to discover the sq. of lengthy kind worth, then we’ve to create one other methodology as follows:

public void longSquare(lengthy quantity)
{ 
lengthy sq. = quantity * quantity;
System.out.printIn("Methodology with lengthy Argument Known as:"+sq.);
}

If we glance rigorously, to search out the sq. of a quantity solely, in response to the information kind of the quantity, we’ve to take three names as follows:

intSquare()

doubleSquare()

longSquare()

Whether it is potential {that a} programmer has to take just one identify and this system itself decides which methodology to make use of for which kind of worth, then it will likely be simpler for the programmer to get the identical. There isn’t any must memorise the names of multiple methodology for kind work. In Java, we may give the above three strategies the identical identify.

If we offer solely the sq. () identify as an alternative of giving totally 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(“Methodology with Integer Argument Known as:“+sq.);
 }
public void Sq.(double quantity)
 {
 double sq. = quantity * quantity; 
System.out.printIn(“Methodology with double Argument Known as:“+sq.);
}
public void Sq.(lengthy quantity)
 { 
lengthy sq. = quantity * quantity;
System.out.printIn(“Methodology with lengthy Argument Known as:“+sq.);
}

If we outline these three strategies in a category, then these strategies might be known as Overloaded Strategies as they’ve the identical identify. Allow us to develop CalculateSquare Class to know this, which is as follows:

class CalculateSquare
 { 
public void sq.()
 { 
System.out.println("No Parameter Methodology Known as");
 } 
public int sq.( int quantity )
 {
int sq. = quantity * quantity;
System.out.println("Methodology with Integer Argument Known as:"+sq.); 
}
public float sq.( float quantity ) 
{
 float sq. = quantity * quantity;
 System.out.println("Methodology with float Argument Known as:"+sq.); 
}
public static void important(String[] args)
  {
    CalculateSquare obj = new CalculateSquare();
    obj.sq.();
    obj.sq.(5);   
    obj.sq.(2.5);   
  }
 }

Notice: Now we have not supplied any argument within the ‘parenthesis’ of the sq.() methodology in our program. On this case, the Compiler Class calls the tactic through which no Parameter has been outlined to realize the Argument.

Output:

No Parameter Methodology Known as 

Methodology with Integer Argument Known as: 25

Methodology with float Argument Known as: 6.25

On this approach, we will outline multiple Strategies of the identical identify in a category, which is known as Methodology Overloading, and 

The Java compiler itself performs the suitable Methodology Name for an object, primarily based on the Information Sort of the Arguments of the Strategies.

Advantages of utilizing Methodology Overloading

  • Methodology overloading will increase the readability of this system.
  • This gives flexibility to programmers in order that they will name the identical methodology for various kinds of information.
  • This makes the code look clear.
  • This reduces the execution time as a result of the binding is finished in compilation time itself.
  • Methodology overloading minimises the complexity of the code.
  • With this, we will use the code once more, which saves reminiscence.

The best way to do Methodology Overloading?

In java, we do methodology overloading in two methods:

  1. By altering the variety of parameters.
  2. By altering information varieties.
  • Change the variety of arguments:

Within the instance beneath, we’ve 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 important(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 varieties: 

Within the instance given beneath, we’ve two strategies, and their information varieties are totally 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 important(String[] args)
{  
System.out.println(Sum.add(17,13));  
System.out.println(Sum.add(10.4,10.6));  
}
}

Notice: On this instance, we’re creating static strategies in order that we don’t must create an occasion for calling strategies.

Output:

30, 21

Some factors to recollect about methodology overloading:

  • Methodology overloading can’t be performed by altering the return kind of strategies.
  • Crucial rule of methodology overloading is that two overloaded strategies should have totally different parameters.

Methodology 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 totally different or not. Which means that 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 important(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 identify in a category known as Methodology Overloading. The Java Compiler itself, primarily based on the Information Sort of the Arguments of the Strategies,  performs the suitable methodology name for an object. 

This brings us to the tip of the weblog on Methodology Overloading in Java. Hope this lets you up-skill your C++ expertise. To be taught extra about programming and different associated ideas, try the programs on Nice Studying Academy

Additionally, in case you are making ready for Interviews, try these Interview Questions for Java to ace it like a professional.

Additionally Learn: The best way to make Resume for Java Developer

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments