-1

Let's say I have some class:

public abstract without sharing MyUnsharingClass {
     // Some methods...
}

which is extended by

public SomeClass extends MyUnsharingClass {
     // Some methods...
}

Would an instance of SomeClass be without sharing?

Or, let's say I have:

public virtual inherited sharing MyInheritingClass {
     public void doSomething() {
        // Some code...
     } 
}

And i have

public virtual without sharing AnotherClass extends MyInheritingClass {
   // This class might be empty for all I care right now.
}

Would invoking doSomething() on an instance of AnotherClass be with or without sharing?

Or would this be any different?

public virtual inherited sharing MyInheritingClass2 {
     public virtual void doSomething() {
        // Some code...
     } 
}

And i have

public virtual without sharing AnotherClass extends MyInheritingClass2 {
     public override void doSomething() {
        super.doSomething();
     } 
}
  • 2
    See also this answer that goes in to a rather lot of detail. The only difference is that it does not cover some of the questions you asked. – sfdcfox Jun 19 '19 at 20:24
7

Would an instance of SomeClass be without sharing?

Yes. Note from Using the with sharing, without sharing, and inherited sharing Keywords

Implementation Details About with sharing and without sharing Keywords

  • The sharing setting of the class where the method is defined is applied, not of the class where the method is called. For example, if a method is defined in a class declared with with sharing is called by a class declared with without sharing, the method executes with sharing rules enforced.
  • If a class isn’t declared as either with or without sharing, the current sharing rules remain in effect. Therefore, the class doesn’t enforce sharing rules except when it acquires sharing rules from another class. For example, if the class is called by another class that has sharing enforced, then sharing is enforced for the called class.
  • Both inner classes and outer classes can be declared as with sharing. The sharing setting applies to all code contained in the class, including initialization code, constructors, and methods.
  • Inner classes do not inherit the sharing setting from their container class.
  • Classes inherit this setting from a parent class when one class extends or implements another.

Again, note:

Classes inherit this setting from a parent class when one class extends or implements another.

  • Thanks for the answer, but this only clearly answers the first of the three questions. If the child class explicitly has a different sharing from the parent, is this always entirely ignored assuming the method is defined on the parent? Or if the parent has "inherit sharing" can it inherit from the child even if the method has no definition on the child but rather was invoked from some third class? – Brian Kessler Jun 19 '19 at 21:13
  • 1
    It is really difficult to answer so many different questions at once. I think it would improve your post to narrow its scope. I didn't even see that you were asking something else there; I will take a closer look. – Adrian Larson Jun 19 '19 at 21:43
  • It's really one question with a few example cases (see title). – Brian Kessler Jun 19 '19 at 21:45
  • 1
    The sharing setting of the class where the method is defined is applied, not of the class where the method is called. So your final two examples would actually behave differently, I believe, in that the penultimate code snippet would retain inherited sharing and the final snippet would run without sharing. – Adrian Larson Jun 19 '19 at 21:48
  • Makes sense, thanks! – Brian Kessler Jun 19 '19 at 21:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.