ACCESS MODIFIERS

To define an apex class specify the following.

1.Access Modifiers:-
  • You must use one of the access modifiers for top level clas (Public or Global).
  • You do not have to use access modifiers in the declaration of inner classes.
  • Optional definition modifiers such as Virtual, Abstract.
  • Required: The keyword Class followed by the class name.
  • Optional extensions: AND/  OR implementation.

      Syntax:

            Private/Public/global[Virtual/abstarct/with sharing/(none)]
            
             Class className[implements InterfaceNameList/ (none)] [extents ClassName/(none)]
                    {
                             //     The body of the class.
                             
                                }

Access Modifiers:-
       
  1.   Private: If you declare a class a private it is only known to the block in which it is                                     declared. By default all the inner classes are private.
       2. Public:-   If you declare a class as a public , this class is visible throughout your application                                   and  you can access the application anywhere.
       
       3.Global:-   If you declare a class as a global . this apex class is visible rtoto all the apex                                          applications in the application or outside the application.


         NOTE:-   If a method or a class(inner) is declare as global then the top level class also must be                              declared as global.

       4.WITH SHARING:  If you declare a class as a with sharing, sharing rules given to the current            user will be taken into the consideration and the users can access & perform the operations                  based on the permissions given to him on objects & fields.(filed level security, sharing rules).

      5.WITHOUT SHARING:- If you declare a class as a without sharing then this apex clas runs in          system mode which means apex code has access to all the objects and fields irrespective of                  current users sharing rules, filed level security, object permissions.

     NOTE:_ 
  •  If the class is not declared as with sharing or without sharing then the class is by default taken as.
  • Both inner Classes and outer Classes can be declared as with sharing.
  • If innerclass is declared as withsharing and top level class is declared as without sharing. Then by default entire context will run in withsharing context.
  • If a class is not declared as with/ without sharing and if this class is called by another class in which sharing is emforced then both the classes run with sharing.
  • Outer class is declared as with sharing and inner class is declared as without sharing then inner class runs in without sharing context only.(inner classes dont take the (sharing properties ) from outer cass).
   6.Virtual:- If a class is declared with keyword virtual then this class can be extended(inherited) or               this cass methods can be overridden by using a class called overridden.

   7.Abstract:- This class contains abstract methods.


  Eg1:- Public class Outerclass
           {
                     // code
                 CLass Innerclass
                 {
                      // Inner Code
                       }
            }



   Eg2:- Public withsharing class sharingclass
             {
                ..// code
                        }

   Eg3:- Public without sharing class nosharing
             {
                ..// code
                        }

     Eg4:- Public with sharing class outer
                    {
                ..// outer class code
                    without sharing class inner
                        
                      {
                   //Inner CLass Code
                             }

                     }

              in the above code outer class runs with current user sharing rules. But inner class runs with                  system context.

        Eg5:- Public with sharing class outer
             {
                ..//outer class  code
                  with sharing class inner
                    {
                    // Inner CLass code
                        }
                 }
             
           In this both inner and outer classes runs with current users permissions.


No comments:

Post a Comment