Core Java - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Overloading in Java

Overloading in Java

shape Description

Overloading in Java, If one class has multiple methods by similar name but, various parameters then it is called as method overloading, the advantage of Overloading in Java consists of the concept about readability of a program but, changing the return type is not possible in method Overloading in Java. Overloading in Java consists of 2 types, such as

shape Conceptual Figure

Following are the types of method overloading.

shape Example

Method Overloading by changing number of parameters.

Overloading in Java, here developer is changing parameters of the program as below. add.java [java]package corejava; public class add { void sum(int a,int 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[]) { add obj=new add(); obj.sum(100,100,100); obj.sum(200,200); } } [/java] Where developer used two methods with the same name that is void sum() and given different parameters such as int a, int b, int c. [java]void sum(int a,int b){System.out.println(a+b);} void sum(int a,int b,int c){System.out.println(a+b+c);} [/java] Where created one object called obj, by using this object method has been called. [java]obj.sum(100,100,100); obj.sum(200,200); [/java] When compile the code output will be as follows. [java] 300 400[/java]

Method Overloading by changing the data type.

Overloading in Java, As explained in the above program where change the data type as double and float then observe the result. [java]package corejava; public class add { void sum(int a,int b) { System.out.println(a+b); } void sum(double a,float b,int c){ System.out.println(a+b+c); } public static void main(String args[]) { add obj=new add(); obj.sum(100,100,100); obj.sum(200,200); } } [/java] Where the developer has changed data type from integer to double and float. [java] void sum(int a,int b) { System.out.println(a+b); } void sum(double a,float b,int c){ System.out.println(a+b+c); } [/java] When compile the code following is the result will be generated. [java] 300.0 400 [/java]

Is method overloading possible by changing the return type.

No, Overloading in Java concept if any one removes the return type, then compile error will become as follows. [java]package corejava; public class add { int sum(int a,int b){ System.out.println(a+b); } double sum(double a,float b,int c){ System.out.println(a+b+c); } public static void main(String args[]){ add obj=new add(); obj.sum(100,100,100); obj.sum(200,200); } } [/java] This code can not be compiled why because it gives compile time error, there is no return type with a name void. [java]int sum(int a,int b) double sum(double a,float b,int c)[/java] Overloading in Java, main() overloading is possible here as follows. [java]public class overload { public static void main(int splesson){ System.out.println(splesson); } public static void main(String...args){ System.out.println("Hello SPLessons"); main(78); } } [/java] In Java one can main method as follows also. [java]public static void main(String...args)[/java] Where main() has been invoked, when compile the code output will be as follows. [java]Hello SPLessons 78[/java]

Overloading vs Overriding in Java

Summary

shape Key Points

  • Overloading in Java is nothing but having same method with various parameters.
  • Overloading is not possible once any one change the return type of the method.