Two or more methods in the same
class with “ SAME NAME ” But their parameter declarations are different, This
is called as overloading.
·
It shows the implementation of polymorphism.
·
Static Polymorphism is achieved by method overloading.
Two Rules of Overloading:
- Argument list must differ.
- Return types cannot be different.
Example Program of Over Loading :
package
org.javanoobs.overloading;
public class CalculateSum {
void findSum (int a, int b){
int twoSum = a+b;
System.out.println(" Result of
int a + int b : " +twoSum);
}
void findsum (int a , double b){
double twoSum = a +b;
System.out.println(" Result of
int a + double b : "+twoSum);
}
void findSum (double a, double b){
double twoSum = a + b;
System.out.println("Result of
double a + double b : " +twoSum);
}
public static void main (String
args[]){
CalculateSum cs = new CalculateSum();
cs.findSum(10, 20);
cs.findsum(10, 20.22);
cs.findSum(22.34, 32.98);
}
}
OUTPUT:
Result of int a + int b : 30
Result of int a + double b : 30.22
Result of double a + double b : 55.31999999999999
No comments:
Post a Comment