When you define an abstract method of a class as public
or global
, an attempt to change the access modifier to protected
or private
when overriding the method produces a compile time error:
Cannot reduce the visibility of method: ...
Similarly for interface methods,
Overriding implementations of global or public interface methods must be global or public: ...
But why is it that Apex allows you to use the public
and global
access modifiers interchangeably?
Are there useful managed package design patterns that involve making a global method public for some subtypes of your class/interface?
Here is an example that the compiler allows:
An abstract class SuperClass
global abstract class SuperClass
{
global abstract void superClassMethod();
}
An interface MyInterface
global interface MyInterface{
void interfaceMethod();
}
A subclass that implements the interface SubClass1
global class SubClass1
extends SuperClass
implements MyInterface
{
// Note the access modifiers
global override void superClassMethod(){ }
global void interfaceMethod(){ }
}
Another subclass that implements the interface SubClass2
global class SubClass2
extends SuperClass
implements MyInterface
{
// Note the access modifiers
public override void superClassMethod(){ }
public void interfaceMethod(){ }
}