oop

14. Polymorphism: Overloading vs Overriding

Analyze the code, write your predicted output, and then reveal the solution.

Scenario

Understand the difference between Compile-time Polymorphism (Overloading) and Runtime Polymorphism (Overriding).

Code Block
class Calculator { // Overloading: Same name, different parameters (Compile-time) int add(int a, int b) { return a + b; } int add(int a, int b, int c) { return a + b + c; } } class ScienceCalculator extends Calculator { // Overriding: Same name, same parameters (Runtime) @Override int add(int a, int b) { System.out.println("Adding in Science Mode"); return a + b; } } public class Main { public static void main(String[] args) { Calculator calc = new ScienceCalculator(); // Compiler decides which 'add' based on parameters (Overloading) System.out.println(calc.add(5, 10, 15)); // JVM decides which 'add' based on object type (Overriding) System.out.println(calc.add(5, 10)); } }
Take Your Shot
Predicted Output

Type exactly what you think will be printed in the console.

14. Polymorphism: Overloading vs Overriding | DevExCode