Public static vs open static vs public class vs open class?
With the introduction of keyword open
in Swift 3 unexpectedly following possible visibility modifiers for the method: open static
, public static
, open class
, public class
, but what are the differences? I understand what the public
equivalent public final
in Java means to override methods and class variables open
, but what does public class func
or mean open static func
? Are they synonymous public static func
? those. will all 3 implementations not allow subclasses to be overridden? Are there unique benefits for each of the 4 different permutations in specific contexts?
source to share
This question is unnecessarily complicated because you are comparing the Cartesian products of two variables ( open
vs public
and static
vs class
), instead of specifying the two variables separately.
It does not matter open static
vs public static
vs open class
vs public class
, but rather open
vs public
and static
vs class
. They come in two orthogonal sizes.
open
vs public
public
:
Within a module, an access specifier public
allows access and override.
From outside the module, the access specifier public
allows access, but does not allow overrides / subclasses.
open
:
Within a module, an access specifier open
allows access and override.
From outside the module, the access specifier open
allows access and allows overrides / subclasses.
static
vs class
static
:
A static
member (method or property) is one who is bound to a specific scope ( class
/ struct
/ enum
) in which it is defined. It's named like this because access to such members is always statically dispatched. This is the Java equivalent static
. Object C is not equivalent to this.
class
:
A class
member is someone who is associated with a class or its subclasses. Members class
can be overridden by subclasses. Because of this, they are dynamically dispatched in the general case, although member access class
can be devirtualized by the optimizer in some cases. Java has no analogues to this. This is equivalent to the Objective C class ( +
) methods .
source to share