What is method overloading with type promotion?

DWQA QuestionsCategory: JavaWhat is method overloading with type promotion?
Editor Staff asked 10 months ago

What is method overloading with type promotion?

1 Answers
Editor Staff answered 10 months ago

By Type promotion is method overloading, we mean that one data type can be promoted to another implicitly if no exact matching is found.
Java Method Overloading with Type Promotion
As displayed in the above diagram, the byte can be promoted to short, int, long, float or double. The short datatype can be promoted to int, long, float or double. The char datatype can be
promoted to int, long, float or double and so on. Consider the following example.
class OverloadingCalculation1{
void sum(int a,long b){System.out.println(a+b);}
void sum(int a,int b,int c){System.out.println(a+b+c);}

public static void main(String args[]){
OverloadingCalculation1 obj=new OverloadingCalculation1();
obj.sum(20,20);//now second int literal will be promoted to long
obj.sum(20,20,20);
}
}
Test it Now
Output
40
60