Write a Java program to demonstrate method overloading with different parameter types and a combination of varargs and specific methods
public class OverloadingWithVarargsAndSpecificMethodsDemo { public static void main(String[] args) { MethodOverloadingDemo calculator = new MethodOverloadingDemo(); int intSum1 = calculator.sum(1, 2, 3); int intSum2 = calculator.sum(2, 4, 6, 8, 10); double doubleSum = calculator.sum(new double[]{1.5, 2.5, 3.5, 4.5, 5.5}); System.out.println("Sum of Three Integers : " + intSum1); System.out.println("Sum of Varargs Integers : " + intSum2); System.out.println("Sum of Double Array : " + doubleSum); } } class MethodOverloadingDemo { int sum(int... numbers)// Method to calculate the sum of integers using varargs { int total = 0; for (int num : numbers) { total += num; } return total; } double sum(double[] numbers)// Method to calculate the sum of an array of doubles { double total = 0.0; for (double num : numbers) { total += num; } return total; } int sum(int a, int b, int c)// Method to calculate the sum of three integers { return a + b + c; } }
Sum of Three Integers : 6 Sum of Varargs Integers : 30 Sum of Double Array : 17.5
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions