Method Overloading in Java With Examples
Strategy Overloading is a component that permits a class to have more than one technique having a similar name, if their contention records are extraordinary. It is like constructor over-burdening in Java, that permits a class to have more than one constructor having distinctive contention records.
we should return direct, when I say contention show it implies the boundaries that a technique has: For instance the contention rundown of a strategy add(int a, int b) having two boundaries is not the same as the contention rundown of the technique add(int a, int b, int c) having three boundaries.
Three Ways to Method Overload:
To over-burden a strategy, the contention arrangements of the techniques should contrast in both of these:
1. Number of boundaries.
For instance: This is a substantial instance of over-burdening
add(int, int)
add(int, int, int)
2. Information kind of boundaries.
For instance:
add(int, int)
add(int, glide)
3. Grouping of Data kind of boundaries.
For instance:
add(int, glide)
add(float, int)
Invalid instance of technique over-burdening:
At the point when I say contention list, I am not discussing return kind of the strategy, for instance if two techniques have same name, same boundaries and have distinctive return type, at that point this is certifiably not a substantial strategy over-burdening model. This will toss accumulation blunder.
int add(int, int)
skim add(int, int)
Technique over-burdening is an illustration of Static Polymorphism. We will examine polymorphism and kinds of it in a different instructional exercise.
Method overLoading Example
As examined in the start of thiss guide, strategy over-burdening is finished by announcing same technique with various boundaries. The boundaries should be diverse in both of these: number, grouping or sorts of boundaries (or contentions). Lets see instances of every one of these cases.
Contention list is otherwise called boundary list
Example 1: Overloading- Differen numbers Parameters in Argument List
This Example of Show how method Overloading is done by having different Number of Parametrs
class DisplayOverloading
{
public void disp(char c)
{
System.out.println(c);
}
public void disp(char c, int num)
{
System.out.println(c + " "+num);
}
}
class Sample
{
public static void main(String args[])
{
DisplayOverloading obj = new DisplayOverloading();
obj.disp('a');
obj.disp('a',10);
}
}
Output:
a
a 10
In the above model – technique disp() is over-burden dependent on the quantity of boundaries – We have two strategies with the name disp however the boundaries they have are extraordinary. Both are having diverse number of boundaries.
Model 2: Overloading – Difference in information kind of boundaries
In this model, strategy disp() is over-burden dependent on the information kind of boundaries – We have two techniques with the name disp(), one with boundary of singe type and another technique with the boundary of int type.
class DisplayOverloading2
{
public void disp(char c)
{
System.out.println(c);
}
public void disp(int c)
{
System.out.println(c );
}
}
class Sample2
{
public static void main(String args[])
{
DisplayOverloading2 obj = new DisplayOverloading2();
obj.disp('a');
obj.disp(5);
}
}
Yield:
a
5
Example3: Overloading – Sequence of information kind of contentions
Here technique disp() is over-burden dependent on grouping of information sort of boundaries – Both the strategies have distinctive arrangement of information type in contention list. First strategy is having contention list as (burn, int) and second is having (int, singe). Since the arrangement is unique, the strategy can be over-burden with no issues.
class DisplayOverloading3
{
public void disp(char c, int num)
{
System.out.println("I'm the primary meaning of technique disp");
}
public void disp(int num, singe c)
{
System.out.println("I'm the second meaning of technique disp" );
}
}
class Sample3
{
public static void main(String args[])
{
DisplayOverloading3 obj = new DisplayOverloading3();
obj.disp('x', 51 );
obj.disp(52, 'y');
}
}
Yield:
I'm the main meaning of strategy disp
I'm the second meaning of strategy disp
Strategy Overloading and Type Promotion
At the point when an information sort of more modest size is elevated to the information kind of greater size than this is called type advancement, for instance: byte information type can be elevated to short, a short information type can be elevated to int, long, twofold and so on
What it has to do with strategy over-burdening?
Indeed, it is vital to comprehend type advancement else you will believe that the program will toss gathering mistake however truth be told that program will run fine due to type advancement.
Lets take a guide to perceive what I am talking here:
class Demo{
void disp(int a, twofold b){
System.out.println("Method A");
}
void disp(int a, twofold b, twofold c){
System.out.println("Method B");
}
public static void main(String args[]){
Demo obj = new Demo();
/* I am passing buoy esteem as a subsequent contention yet
* it got elevated to the sort twofold, in light of the fact that there
* wasn't any technique having arg list as (int, skim)
*/
obj.disp(100, 20.67f);
}
}
Yield:
Technique A
As you can see that I have passed the buoy esteem while calling the disp() strategy however it got elevated to the twofold sort as there wasn't any technique with contention list as (int, glide)
Yet, this sort advancement doesn't generally occur, lets see another model:
class Demo{
void disp(int a, twofold b){
System.out.println("Method A");
}
void disp(int a, twofold b, twofold c){
System.out.println("Method B");
}
void disp(int a, glide b){
System.out.println("Method C");
}
public static void main(String args[]){
Demo obj = new Demo();
/* This time advancement will not occur as there is
* a technique with arg list as (int, drift)
*/
obj.disp(100, 20.67f);
}
}
Yield:
Technique C
As you see that this time type advancement didn't occur in light of the fact that there was a technique with coordinating with contention type.
Type Promotion table:
The information type on the left side can be elevated to the any of the information type present in the correct side of it.
byte → short → int → long
short → int → long
int → long → skim → twofold
glide → twofold
long → glide → twofold
Lets see not many Valid/invalid instances of technique over-burdening
Case 1:
inft mymethod(int a, int b, glide c)
int mymethod(int var1, int var2, glide var3)
Result: Compile time mistake. Contention records are by and large same. The two techniques are having same number, information types and same arrangement of information types.
Case 2:
int mymethod(int a, int b)
int mymethod(float var1, skim var2)
Result: Perfectly fine. Substantial instance of over-burdening. Here information sorts of contentions are extraordinary.
Case 3:
int mymethod(int a, int b)
int mymethod(int num)
Result: Perfectly fine. Substantial instance of over-burdening. Here number of contentions are unique.
Case 4:
drift mymethod(int a, skim b)
drift mymethod(float var1, int var2)
Result: Perfectly fine. Legitimate instance of over-burdening. Grouping of the information kinds of boundaries are extraordinary, first strategy is having (int, buoy) and second is having (drift, int).
Case 5:
int mymethod(int a, int b)
glide mymethod(int var1, int var2)
Result: Compile time mistake. Contention records are actually same. Despite the fact that return kind of techniques are unique, it's anything but a legitimate case. Since return sort of strategy doesn't make any difference while over-burdening a technique.
Supposition the appropriate responses prior to checking it toward the finish of projects:
Question 1 – return type, technique name and contention list same.
class Demo
{
public int myMethod(int num1, int num2)
{
System.out.println("First myMethod of class Demo");
return num1+num2;
}
public int myMethod(int var1, int var2)
{
System.out.println("Second myMethod of class Demo");
return var1-var2;
}
}
class Sample4
{
public static void main(String args[])
{
Demo obj1= new Demo();
obj1.myMethod(10,10);
obj1.myMethod(20,12);
}
}
Answer:
It will toss an aggregation mistake: More than one technique with same name and contention list can't be characterized in an equivalent class.
Question 2 – return type is extraordinary. Technique name and contention list same.
class Demo2
{
public twofold myMethod(int num1, int num2)
{
System.out.println("First myMethod of class Demo");
return num1+num2;
}
public int myMethod(int var1, int var2)
{
System.out.println("Second myMethod of class Demo");
return var1-var2;
}
}
class Sample5
{
public static void main(String args[])
{
Demo2 obj2= new Demo2();
obj2.myMethod(10,10);
obj2.myMethod(20,12);
}
}








0 Comments:
Post a Comment