Write a Java program to demonstrate method overloading with different parameter types and generic methods
This Java program demonstrates method overloading with generics. It defines a class MethodOverloadingDemo with overloaded add methods for adding integers, concatenating strings, and a generic method to add two values of any type. Another class OverloadingWithGenericsDemo is used to demonstrate these overloaded methods.
public class OverloadingWithGenericsDemo { public static void main(String[] args) { MethodOverloadingDemo calculator = new MethodOverloadingDemo(); int sumInt = calculator.add(10, 20); String concatStr = calculator.add("Hello ", "World"); Object result = calculator.add(3.7, 10.5); // Using generic method System.out.println("Sum of Integers : " + sumInt); System.out.println("Concatenation of Strings : " + concatStr); System.out.println("Generic Result : " + result); } } class MethodOverloadingDemo { int add(int a, int b)// Method to add two integers { return a + b; } String add(String a, String b)// Method to concatenate two strings { return a + b; } <T> T add(T a, T b) // Generic method to add two values of any type { return a; } }
Sum of Integers : 30 Concatenation of Strings : Hello World Generic Result : 3.7
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions