ApexSensei

Drills › Think in classes

Overload a Case label method

Think in classes · Intermediate · 15 min · Runs in your Salesforce Developer org · Free

Scenario. A Case list needs one easy-to-read label method that accepts different kinds of detail.

Give one method several parameter lists for normal, urgent, and aged Cases.

What you will learn

Overloading means one method name with different parameter lists. Apex picks the version whose parameters match the arguments you pass.

String label(String subject)
String label(String subject, Boolean urgent)

One overload can call another, like return label(subject);, to reuse its logic.

Gotcha. Changing only the return type does not create an overload. The parameter types must differ.

The task

Overload a Case label method so each parameter list handles normal, urgent or aged Cases.

Examples:

  • label('Password reset') → printed as Default: Normal: Password reset
  • label('Data loss', true) → printed as With flag: Urgent: Data loss. With false it returns Normal: Data loss.
  • label('Pending refund', 7) → printed as With age: Aged: Pending refund. With 6 it returns Normal: Pending refund.

Requirements:

  1. Keep label(String subject). It returns Normal: plus the subject.
  2. Make the Boolean version return Urgent: plus the subject when the flag is true, and the normal label when it is false.
  3. Add an Integer version that returns Aged: plus the subject at 7 days or more, and the normal label below 7.
  4. Print Default:, With flag: and With age:, each followed by the matching call from the examples.
  5. The checks also call new CaseLabeler().label(...) with other inputs, so do not hard-code any answer.

Starter code

class CaseLabeler {
    public String label(String subject) {
        return 'Normal: ' + subject;
    }

    public String label(String subject, Boolean urgent) {
        return label(subject);
    }
}

CaseLabeler labeler = new CaseLabeler();
System.debug('Default: ' + labeler.label('Password reset'));

When it passes

Apex picks each overload by its arguments. Urgent and aged Cases get their own label, and everything else falls back to the normal label, including exactly 6 days.

Try this drill free

You write the Apex yourself. ApexSensei runs it and tells you what passed and what did not.

‹ Expose a read-only quote total · Route Cases with an enum ›