• Lessons Home
Topics
Community
  1. Lessons
  2. Apex Syntax Fundamentals
  3. Methods and Functions
  4. Method Overloading

      Method Overloading

      Method Overloading

      Method overloading means defining multiple methods with the same name but different parameter lists. Apex looks at the arguments you pass and picks the version that matches.

      What Makes Methods Different

      The compiler tells overloaded methods apart by their signature: the method name plus the number, types, and order of parameters. Two overloads must differ in their parameter list:

      public static String format(Integer value) {
          return 'Number: ' + value;
      }
      
      public static String format(String value) {
          return 'Word: ' + value;
      }
      
      public static String format(String value, Integer times) {
          return value + ' x' + times;
      }
      

      All three methods share the name format, but each has a different parameter list, so all three can exist together.

      Return Type Is Not Enough

      Changing only the return type does not create a valid overload. If two methods have the same name and the same parameter list, the code does not compile — even if one returns String and the other returns Integer.

      How Apex Picks a Version

      When you call an overloaded method, Apex matches your arguments to the parameter lists:

      String a = format(42);        // calls the Integer version
      String b = format('hello');   // calls the String version
      String c = format('go', 3);   // calls the two-parameter version
      

      Why Overloading Helps

      Overloading lets one concept keep one name, no matter what shape the input takes. Callers always call format, and the language routes each call to the right version, which keeps your code small and easy to remember.

      Apex Code Editor
      Sign in to Submit

      Welcome to Lightning Challenge!

      How It Works

      • • Write your solution in the code editor
      • • Connect your Salesforce org to test
      • • Submit to check if your solution passes
      • • Use hints if you get stuck

      Note

      Complete this lesson challenge to earn points and track your progress. The code editor allows you to implement your solution, and the tests will verify if your code meets the requirements.

      Wally Assistant

      Wally can't hear you

      Please sign in to access the AI Assistant

      Sign In