Create a Java program to demonstrate method overriding with a subclass that overrides a method from an interface and implements a method with the same name but different parameters
Interface MathOperations with a method calculate, and a class Calculator that implements this interface. The Calculator class provides two implementations of the calculate method using method overloading. Here's an explanation of the code:
interface MathOperations { int calculate(int a, int b); } class Calculator implements MathOperations { @Override public int calculate(int a, int b) { return a + b; } int calculate(int a, int b, int c) { return a + b + c; } } public class Main { public static void main(String[] args) { Calculator calculator = new Calculator(); System.out.println("Sum of Two Numbers : " + calculator.calculate(4, 7)); System.out.println("Sum of Three Numbers : " + calculator.calculate(10, 20, 30)); } }
Sum of Two Numbers : 11 Sum of Three Numbers : 60
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions