Method Overloading
Many peoples acquire confused betwixt method overloading together with method overriding inwards coffee software evolution language. Earlier nosotros learnt near method overriding inwards THIS POST. Method overloading concept is totally unlike than method overriding. If interviewer asks yous question near method overloading together with then your respond should move similar this "When cast direct keep 2 or to a greater extent than methods amongst same cite but unlike parameters, it is called method overloading" inwards coffee software evolution language. Lets own to sympathise method overloading concept amongst example.
Method Overloading Example
We tin hit method overloading past times changing expose of arguments or past times changing information types of arguments for same cite methods inwards unmarried class.
Method overloading past times changing expose of arguments
In bellow given instance yous tin run into that nosotros direct keep created 2 overloaded methods. 1st amount method volition convey 2 arguments together with sec amount method volition convey iii arguments to perform amount operation. This is called method overloading past times changing expose of arguments.
public cast OverloadingByArgument { void Sum(int i, int j) { System.out.println(i + j); } void Sum(int i, int j, int k) { System.out.println(i + j + k); } world static void main(String args[]) { OverloadingByArgument ol = novel OverloadingByArgument(); ol.Sum(10, 10, 10); //It volition telephone outcry upward Sum(int i, int j, int k) ol.Sum(20, 20);//It volition telephone outcry upward Sum(int i, int j) } }
OutPut
30 40
Method overloading past times changing information types of arguments
You tin also overload method past times changing information types of argument. Here expose of arguments tin move same or unlike equally per requirement. In bellow given example, Class direct keep 2 amount methods amongst same expose of arguments but unlike datatypes. 1st amount method volition move called when yous wants to amount 2 integer values together with sec amount method volition move called when yous wants to amount 2 double values. You tin purpose whatsoever other datatypes too.
public cast OverloadingByDataTypes { void Sum(int i, int j) { System.out.println(i + j); } void Sum(double i, double j) { System.out.println(i + j); } world static void main(String args[]) { OverloadingByDataTypes ol = novel OverloadingByDataTypes(); ol.Sum(10.75, 10.5); //It volition telephone outcry upward Sum(double i, double j) ol.Sum(20, 20);//It volition telephone outcry upward Sum(int i, int j) } }
OutPut
21.25 40
Advantage of Method Overloading
Using method overloading, We tin practise multiple methods amongst same cite inwards same cast to perform same activity inwards unlike ways (using unlike expose of arguments together with it's datatypes). It will increases the readability of the software program. We tin overload static methods too. stance to a greater extent than java software evolution language tutorials on THIS PAGE.