Object Oriented Programming - JAVA - Method Overloading
Object Oriented Programming - JAVA 👽
In Java, sometimes we have to use methods with the same name. So how do we use such methods without any errors? Today we are talking about these situations.
Method Overloading.....
What is method overloading? When it is necessary to have several methods with the same name in the same class, by changing the parameters of those methods, it is possible to keep those methods as separate methods. This practice is called method overloading. This method overloading also known as signature changing.
When we overload the method, there are three basic things we need to take care of the parameters.
- Number of parameters
- Datatype of parameters
- Sequence of parameters
Let's look at examples of changing parameters.
Number of parameters :
class A {
void display (int a){
// only one parameters
}
void display (int x , int y){
// two parameters
}
}
Datatype of parameters :
class A {
void display (String a){
// String datatype parameters
}
void display (int x , char y){
// integer and char datatype parameters
}
}
Sequence of parameters :
class A {
void display (String x , int y){
// String, integer datatype parameters
}
void display (int x , String y){
// integer, String datatype parameters
}
}
We can pass the parameter values through the object as we learned. We can never overload the main method.
Comments
Post a Comment