Difference between public, private, protected and global access modifiers in Salesforce?

Difference between public, private, protected and global access modifiers in Salesforce?

private is the default, and means that the method or variable is accessible only within the Apex class in which it is defined.  If you do not specify an access modifier, the method or variable is private.

We can access global class anywhere irrespective of namespace. If you declare a method or variable as global, you must also declare the class that contains it as global.

Public class is accessible within namespace only.

Protected means that the method or variable is visible to any inner classes in the defining Apex class, and to the classes that extend the defining Apex class. You can only use this access modifier for instance methods and member variables.

Check the below link for more information.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_access_modifiers.htm

Leave a Reply