Inherited Sharing vs No Sharing declaration
When inherited sharing is used, it means that it will inherit the sharing settings of the caller class.
- If a class with
without sharing
is calling the method of class withinherited sharing
, then it will run inwithout sharing
mode. - If a class with
with sharing
is calling the method of class withinherited sharing
, then it will run inwith sharing
mode. - If a class with
omitted sharing
(i.e. no sharing is mentioned) is calling the method of class withinherited sharing
, then it will run inwithout sharing
mode as the default sharing mode of theomitted sharing
is to run inwithout sharing
mode (This is because the calling class is established as awithout sharing
context). A class declared asinherited sharing
runs aswithout sharing
only when explicitly called from an already establishedwithout sharing
context. - If a class with
with sharing
is calling a method of class withomitted sharing
which in turn calls a method of class withinherited sharing
, then it will run inwith sharing
mode, as the calling class is inwith sharing
mode and omitted class is established aswith sharing
context. - If a class with
without sharing
is calling a method of class withomitted sharing
which in turn calls a method of class withinherited sharing
, then it will run inwithout sharing
mode, as the calling class is inwithout sharing
mode and the omitted class is established aswithout sharing
context. - If the class is used as the entry point to an Apex transaction, an
omitted sharing
declaration runs aswithout sharing
. - If the class is used as the entry point to an Apex transaction,
inherited sharing
ensures that the default is to run aswith sharing
.
Excerpt from the salesforce documentation, Using the with sharing, without sharing, and inherited sharing Keywords
There is a distinct difference between an Apex class that is marked with inherited sharing and one with an omitted sharing declaration. If the class is used as the entry point to an Apex transaction, an omitted sharing declaration runs as without sharing. However, inherited sharing ensures that the default is to run as with sharing. A class declared as inherited sharing runs as without sharing only when explicitly called from an already established without sharing context.
Hope answers for both your questions are covered in the above explanation.
If you can refer salesforce docs then you observed the difference between inherited sharing and no sharing. Excerpt from salesforce docs:
There is a distinct difference between an Apex class that is marked with inherited sharing and one with an omitted sharing declaration. If the class is used as the entry point to an Apex transaction, an omitted sharing declaration runs as without sharing. However, inherited sharing ensures that the default is to run as with sharing. A class declared as inherited sharing runs as without sharing only when explicitly called from an already established without sharing context.
Please refer below link for more details:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_keywords_sharing.htm