5

After upgrading our source from 60.0 to 61.0, I observed regression in some of our libraries. After investigation, I noticed that api version 61.0 breaks (changes?) the way, inheritance for inner classes worked.

In 60 and below, you can declare an inner classes with private virtual methods and override these methods in another inner class (this only works for inner classes in the same apex class for obvious reasons). When instantiating the subclass polymorphic (e.g. assinging to a variable of Superclass, it correctly calls the overwritten method, not theS ubclass. This has changed in 61.

I can consistently reproduce this behaviour with anonymous apex and deployed code. Take this example code and run it with --api-version 60.0 and --api-version 61.0.

public class Superclass1 {
    private virtual String myMethod() {
        return 'Superclass1';
    }
}
public class Subclass2 extends Superclass1 {
    private override String myMethod() {
        return 'Subclass2';
    }
}
Superclass1 instanceOf1 = new Superclass1();
Superclass1 instanceOf2 = new Subclass2();

System.debug('Class 1: ' + instanceOf1.myMethod());
System.debug('Class 2: ' + instanceOf2.myMethod());

Output for 60.0 (redacted for readability)

60.0 APEX_CODE,DEBUG;APEX_PROFILING,INFO
05:26:06.34 (34414904)|USER_INFO|[EXTERNAL]|0059K00000BZZ6D|[email protected]|(GMT-07:00) Pacific Daylight Time (America/Los_Angeles)|GMT-07:00
05:26:06.34 (34436723)|EXECUTION_STARTED
05:26:06.34 (34444786)|CODE_UNIT_STARTED|[EXTERNAL]|execute_anonymous_apex
05:26:06.34 (35208995)|USER_DEBUG|[16]|DEBUG|Class 1: Superclass1
05:26:06.34 (40878759)|USER_DEBUG|[17]|DEBUG|Class 2: Subclass2
05:26:06.40 (40985945)|CUMULATIVE_LIMIT_USAGE
05:26:06.40 (40985945)|LIMIT_USAGE_FOR_NS|(default)|
  [...]
05:26:06.40 (40985945)|CUMULATIVE_LIMIT_USAGE_END

05:26:06.34 (41017930)|CODE_UNIT_FINISHED|execute_anonymous_apex
05:26:06.34 (41031865)|EXECUTION_FINISHED

Same output, run on the same org, but with --api-version 61.0

61.0 APEX_CODE,DEBUG;APEX_PROFILING,INFO
05:26:13.42 (42756298)|USER_INFO|[EXTERNAL]|0059K00000BZZ6D|[email protected]|(GMT-07:00) Pacific Daylight Time (America/Los_Angeles)|GMT-07:00
05:26:13.42 (42787091)|EXECUTION_STARTED
05:26:13.42 (42794748)|CODE_UNIT_STARTED|[EXTERNAL]|execute_anonymous_apex
05:26:13.42 (43802330)|USER_DEBUG|[16]|DEBUG|Class 1: Superclass1
05:26:13.42 (43847036)|USER_DEBUG|[17]|DEBUG|Class 2: Superclass1
05:26:13.43 (43959585)|CUMULATIVE_LIMIT_USAGE
05:26:13.43 (43959585)|LIMIT_USAGE_FOR_NS|(default)|
  [...]

05:26:13.43 (43959585)|CUMULATIVE_LIMIT_USAGE_END

05:26:13.42 (44019892)|CODE_UNIT_FINISHED|execute_anonymous_apex
05:26:13.42 (44033182)|EXECUTION_FINISHED

More investigation using the Apex Replay Debugger shows, that the code enters the Subclass2 (in v60 and earlier), but enters Subclass1 (in v61).

Is this intended behaviour? Is this a bug?

1 Answer 1

6

In case someone just came here for the fix: Setting the visibility of the virtual method to minimum protected solves the problem.

This is documented in the Summer '24 release notes, thus:

In API version 61.0 and later, private methods are no longer overridden by an instance method with the same signature in a subclass. This change is versioned, so to prevent the override, update your abstract or virtual classes that contain private methods to API version 61.0 or later. In API version 60.0 and earlier, if a subclass declares an instance method with the same signature as a private method in one of its superclasses, the private method is overridden by the method in the subclass.

2
  • 3
    The key here is that this is specifically documented in the Summer '24 release notes.
    – Phil W
    Commented Aug 27 at 12:53
  • 1
    Thank you very much! I was looking for this specific change, but apparently it wasn't indexed so search engine did not find this for me. And I did not consider researching the release notes. I find it odd, that this does not throw a compile error, though. You usually cannot use override for a method that can't be overwritten. Commented Aug 27 at 12:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .