PDI · Question #239
A developer wrote the following two classes: The StatusFetcher class successfully compiled and saved. However, the Calculator class has a compile time error. How should the developer fix this code?
The correct answer is B. Make the isActive method in the StatusFetcher class public. To fix the compile-time error in the Calculator class attempting to call a method from StatusFetcher, the isActive method in StatusFetcher must be declared as public to allow external access.
Question
A developer wrote the following two classes:
The StatusFetcher class successfully compiled and saved. However, the Calculator class has a compile time error. How should the developer fix this code?
Exhibit
Options
- AChange the class declaration for the statusFetcher class to public with inherited sharing.
- BMake the isActive method in the StatusFetcher class public.
- CMake the doCalculations method in the Calculation class private.
- DChange the class declaration for the Calculator class to public with inherited sharing.
How the community answered
(36 responses)- B92% (33)
- C3% (1)
- D6% (2)
Why each option
To fix the compile-time error in the Calculator class attempting to call a method from StatusFetcher, the `isActive` method in StatusFetcher must be declared as public to allow external access.
Changing the class declaration to `public with inherited sharing` controls record sharing context, which is unrelated to the compile-time error caused by insufficient method visibility between classes.
For the Calculator class to invoke the `isActive()` method of the StatusFetcher class, the `isActive()` method must be declared with the `public` access modifier, allowing it to be visible and callable from other classes. By default, methods without an explicit access modifier are private.
Making the `doCalculations` method private would restrict access to `doCalculations` itself but would not resolve the compile-time error occurring within `doCalculations` when attempting to call an inaccessible method from StatusFetcher.
Changing the Calculator class declaration to `public with inherited sharing` controls record sharing, which does not address the method accessibility issue between the two classes.
Concept tested: Apex class and method access modifiers
Source: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_access_modifiers.htm
Topics
Community Discussion
No community discussion yet for this question.
