Differences between apex sharing keywords

Hope this post will help you to understand the differences between different apex sharing keywords.

Inherited sharing vs. with sharing vs. without sharing:

with sharing: By default apex class runs in system context mode (means objects/field permissions and sharing-rules are not applied for the current user). If a class defined as with sharing, it enforces user permissions and sharing rules.

Syntax to define with sharing:

public with sharing class ExamplewithsharingClass {
// write your code.

without sharing: If you define a class with this keyword, sharing rules for the current user is not enforced. for example, you can use this keyword if you want to turn off sharing-rule enforcement when a class is called from other class that is defined using with sharing keyword.
If you don’t define apex class with any keyword (with sharing or without sharing), it runs in system mode i.e. without sharing mode.

Syntax to define without sharing:

public without sharing class ExamplewithoutsharingClass{
//write your code.

inherited sharing: Use inherited sharing keyword to run the apex class in sharing-mode of the class that is called it.
Syntax to define inherited sharing:

public without sharing class ExampleinheritedsharingClass{
//write your code.