Wednesday, April 9, 2014

What is Access Modifier and What are the different Access Modifiers available in Apex?

What is Access Modifier and What are the different Access Modifiers available in Apex?



The access to classes, constructors, methods and fields are regulated using access modifiers i.e. a class can control what information or data can be accessible by other classes. To take advantage of encapsulation, you should minimize access whenever possible.

Apex provides a number of access modifiers to help you set the level of access you want for classes as well as the fields, methods and constructors in your classes. A member has package or default accessibility when no accessibility modifier is specified.

private

This 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.

protected

This means that the method or variable is visible to any inner classes in the defining Apex class. You can only use this access modifier for instance methods and member variables. Note that it is strictly more permissive than the default (private) setting, just like Java.

public

This means the method or variable can be used by any Apex in this application or namespace.

Note :- In Apex, the public access modifier is not the same as it is in Java. This was done to discourage joining applications, to keep the code for each application separate. In Apex, if you want to make something public like it is in Java, you need to use the global access modifier.

global

This means the method or variable can be used by any Apex code that has access to the class, not just the Apex code in the same application. This access modifier should be used for any method that needs to be referenced outside of the application, either in the SOAP API or by other Apex code. If you declare a method or variable as global, you must also declare the class that contains it as global.

Access Modifiers on Classes
Classes have two access modifiers:

1. public: The class is visible to the entire application namespace, but not outside it.

2. global: The class is visible to Apex code running in every application namespace. If an inner class is global, its outer class is required to be global.

Access modifiers on outer classes are required. Inner classes are private by default, accessible only by their containing classes.

Access Modifiers on Methods and Variables 

Methods and variables have four access modifiers:

1. private: The method or variable is visible only within its defining class.

2. protected: It is visible to the defining class and subclasses.

3. public: It is visible to any Apex code in the same application namespace but not accessible to other namespaces.

4. global: It can be used by any Apex code running anywhere in the organization, in any namespace. If a method is global, its class must be global as well.

If no access modifier is provided, the method or variable is private



2 comments:

Gulzar said...

Types of Access modifiers are explained very excellent in this short blog.These modifiers are used in Java programming in a different sense.

Vinoth said...

Thanks a heep for sharing this concept

Post a Comment

 
| ,