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();
     } 
}
Brian Kessler
  • 4,558
  • 2
  • 25
  • 66

1 Answers1

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.

Adrian Larson
  • 149,971
  • 38
  • 239
  • 420
  • 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
  • I'm worried that your reliance on the last bullet point is going to conflict with the first bullet point. if class A extends class B, then the sharing policy will be picked up from class A OR class B depending only on where the method is defined. Therefore in some sense sharing completely ignores class hierarchy, it only looks at the sharing declaration of the class where the method is defined, and this is the principle you should follow rather than focusing on inheritance, which only comes into play as the method may be defined elsewhere. – Robert Sussland May 01 '21 at 21:41