function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Himanshu GhateHimanshu Ghate 

base class is declared as with sharing and derived class is declared as without sharing what will happen?

SwethaSwetha (Salesforce Developers) 
HI Himanshu,
If a base class is declared with the with sharing keyword and a derived class is declared with the without sharing keyword, the derived class will run in system mode, which means that Apex code has access to all the objects and fields irrespective of the current user's sharing rules, field-level security, and object permissions.

However, note that the sharing setting of the class where the method is defined is applied, not of the class where the method is called

So, if a method is defined in a class with the with sharing keyword but is called in a class without sharing, the method will execute with sharing rules enforced. Classes inherit the sharing setting from their parent class when one class extends from another class

Inner classes do not inherit the sharing setting from the outer class.

Example:
public with sharing class BaseClass {
    public void doSomething() {
        // Perform some operation
    }
}

public without sharing class DerivedClass extends BaseClass {
    public void doSomethingElse() {
        // Perform some other operation
    }
}

// In some other class
public void someMethod() {
    DerivedClass d = new DerivedClass();
    d.doSomething(); // This will execute with sharing rules enforced
    d.doSomethingElse(); // This will execute in system mode
}

See related: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_keywords_sharing.htm
https://salesforce.stackexchange.com/questions/203980/sharing-without-sharing-keywords-inheritance-behaviour

If this information helps, please mark the answer as best. Thank you
Arun Kumar 1141Arun Kumar 1141
Hi Himanshu,

When a base class is declared with the "with sharing" keyword, it enforces the sharing rules of the current user's profile or organization-wide defaults. This means that the code in the base class will respect the sharing rules and access restrictions defined for the objects it interacts with.

If a derived class is declared as "without sharing," it ignores the sharing rules enforced by the base class and allows the code in the derived class to access and modify records regardless of the user's profile or organization-wide sharing defaults. Essentially, the "without sharing" keyword overrides the sharing behavior inherited from the base class.

In summary, when a base class is declared with "with sharing" and a derived class is declared with "without sharing," the derived class will not adhere to the sharing rules enforced by the base class. It will have broader access privileges and can perform operations that would otherwise be restricted by sharing rules, potentially leading to security risks or unintended data exposure. Care should be taken when using "without sharing" to ensure that the code is implemented in a way that aligns with the desired security and sharing requirements.

If this helps you please mark it as a best answer,
Thank!