Overloaded Methods Example
This program demonstrates method overloading.
Problem Statement
Write a Java program to overload add() method with different parameter types.
Source Code
| 1 | class Calculator { |
| 2 | int add(int a,int b){ return a+b; } |
| 3 | double add(double a,double b){ return a+b; } |
| 4 | } |
| 5 | public class MethodOverloading { |
| 6 | public static void main(String[] args) { |
| 7 | System.out.println("Eduinq Method Overloading"); |
| 8 | Calculator c=new Calculator(); |
| 9 | System.out.println("Sum (int): "+c.add(5,10)); |
| 10 | System.out.println("Sum (double): "+c.add(5.5,10.5)); |
| 11 | } |
| 12 | } |
Program Output
Eduinq Method Overloading Sum (int): 15 Sum (double): 16.0
Explanation
Method overloading allows multiple methods with same name but different parameter lists. - Compiler resolves which method to call based on argument types. - This improves readability and flexibility, letting developers use intuitive method names for different data types. Conclusion: Shows how to overload methods in Java.