4.A] Write a program using a Generic Interface.
Answer:
// Define a generic interface interface SimpleCalculator<T> { T add(T a, T b); T subtract(T a, T b); } // Implement the generic interface for Integer type class IntegerCalculator implements SimpleCalculator<Integer> { @Override public Integer add(Integer a, Integer b) { return a + b; } @Override public Integer subtract(Integer a, Integer b) { return a - b; } } // Main class to test the implementation public class Main { public static void main(String[] args) { // Use the IntegerCalculator SimpleCalculator<Integer> calculator = new IntegerCalculator(); // Perform addition and subtraction System.out.println("Addition: " + calculator.add(2, 3)); // Output: 5 System.out.println("Subtraction: " + calculator.subtract(5, 3)); // Output: 2 } }
Output:
Addition: 5 Subtraction: 2