In this blog post,we will take a look at different types of access modifiers which are available in Apex programming.
Apex has 4 different types of access modifiers.
Let’s discuss them one by one.
The method or variable with global access modifier can be used by any apex code that has access to the apex class,not just the apex code in the same application. If you want a method which can be referenced outside of the application, either in SOAP API or by other apex code, in that case you will use global access modifier. If you have declared a method or variable with global keyword, you must declare the class with global keyword as well.
if you are using public access modifier with a method or variable, that means that variable or method can by used by
any apex in this application or namespace. Public access modifier in apex is not similar to the one in java. If you want to make something public like java, in that case you have to use global access modifier.
If you are using protected access modifier with a variable or method then, that method or variable is visible to any inner classes in the defining apex class and also visible to the classes that extend from the defining apex class. You can use this protected modifier with instance methods and member variables.
This is the default access modifier and the method or variable defined with private access modifier is only visible inside the apex class in which they are defined. When you don’t specify any access modifier then private is the default access modifier.
To learn more: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_access_modifiers.htm