Method Overloading. In English
Method overloading
What is method overloading? ............
Method overloading is when there are several methods with the same name in the same class, the arguments of the parameter signature are changed, and the methods are introduced separately.
Ex: -
30
Ways of Method Overloading ...............
There are three ways of Method Overloading.
1.Changing the number of parameters
2.Changing data types of the arguments
3.Changing the order of the parameters of methods
1.Changing the number of parameters
Here method overloading can be done by changing the number of parameters while passing to different methods.
Ex: -
import java.io.*;
class Computer {
public int multiply(int x, int y) {
int comp = x * y;
return comp;
}
public int multiply(int x, int y, int z) {
int comp = x * y * z;
return comp;
}
}
class Phone {
public static void main (string[]args) {
Computer max = new Computer();
int comp1 = max.multiply(10,20);
int comp2 = max.multiply(10,20,30);
System.out.println("Two integer value:" + comp1);
System.out.println("Three integer value:" + comp2);
}
}
Output: - Two integer value: 200
Three integer value: 6000
2.Changing data types of the arguments
If methods have the same name but different parameter types, the methods can be considered overloaded.
Ex: -
import java.io.*;
class Computer {
public int comp(int x, int y) {
int comp1 = x * y;
return comp1;
}
public double comp(double x, double y) {
double comp2 = x * y;
return comp2;
}
}
class Phone {
public static void main(string[]args) {
Computer max = new Computer();
int comp1 = max.comp(10,20);
double comp2 = max.comp(10.0,20.0);
System.out.println("Integer value: "+ comp1);
System.out.println("Double value: "+ comp2);
}
}
Output: - Integer value: 200
Double value: 200.0
3.Changing the order of the parameters of methods
Method overloading can also be implemented by resetting the parameters of two or more overloaded methods. For example, if method1 has parameters (string name, int index_no) and the other method (int index_no, string name), but both have the same name, these 2 methods will be overloaded with different parameter orders.
Ex: -
import java.io.*;
class School {
public void Student(string name, int index_no) {
System.out.println("Name:"+ name + " " + "Index_No:" + index_no);
}
Public void Student(int index_no, string name) {
System.out.printiln("Index_No:" + index_no +"Name:" + name);
}
}
class Section {
public static void main(string[]args) {
School max = new School();
max.Student("Nimeshi",00173);
max.student(00122, "Sathya");
}
}
Output: - Name: Nimeshi Index_No: 00173
Index_ No: 00122 Name: Sathya
Disadvantages of method overloading ...................
- It can be challenging for a complete novice to comprehend the method overloaded procedure.
- The number of parameters and their data types need to be designed and finalized with additional effort.
Advantages of method overloading .........................
- Method overloading makes the software easier to read and reuse.
- The program's complexity is decreased by method overloading.
- Programmers can complete a task quickly and accurately by using method overloading.
- Accessing methods that execute related functions with slightly different arguments and types is achievable using method overloading.
- A class's constructors can also be used to initialize objects in a variety of ways.
Comments
Post a Comment