Bird
0
0

Find the error in this code snippet:

medium📝 Debug Q14 of 15
Java - Methods and Code Reusability

Find the error in this code snippet:

class Calculator {
  int add(int a, int b) { return a + b; }
  double add(double a, double b) { return a + b; }
  int add(int a, int b, int c) { return a + b + c; }
  int add(int a, int b) { return a - b; }
}
ADuplicate method add(int a, int b) with different body causes error
BOverloading requires different method names
CReturn type difference causes error
DNo error, code compiles fine
Step-by-Step Solution
Solution:
  1. Step 1: Check method signatures

    Two methods have signature add(int a, int b) but different bodies.
  2. Step 2: Understand overloading rules

    Overloading requires unique parameter lists; duplicate signatures cause errors regardless of body.
  3. Final Answer:

    Duplicate method add(int a, int b) with different body causes error -> Option A
  4. Quick Check:

    Same parameters = duplicate method error [OK]
Quick Trick: Same parameters, different body = error, not overloading [OK]
Common Mistakes:
  • Thinking different return types fix duplicates
  • Ignoring duplicate parameter lists
  • Confusing overloading with overriding

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes