The other day, I used it in a smartphone app with the title "Access modifier summary-Smartphone application development (Java / Kotlin / Objective-C / Swift / C # / F #)" I have summarized the access modifier comparison of languages, but this time I would like to summarize the access modifiers with the theme of "JVM language".
It is a language that runs on the JVM (Java Virtual Machine), and there are many Java alternative languages in addition to Java. This time, we will take up the major Groovy, Scala, and Kotlin among them and summarize them in a comparison table together with Java.
Access modifier | Java | Groovy | Scala | Kotlin |
---|---|---|---|---|
public | Accessable from anywhere | Accessable from anywhere | Accessable from anywhere | Accessable from anywhere |
protected | Accessable from class + derived class in the same package | Accessable from anywhere | Accessable from the same class + derived class | 継承したAccessable from class + derived class in the same package |
internal | (not exist) | (not exist) | (not exist) | Accessable within the same module |
private | Accessable within the same scope * You can also refer to the private variable of the inner class. |
Accessable from anywhere * However, super from the derived class.*Cannot be accessed with |
Accessable within the same scope | Accessable within the same scope |
protected[package name] | (not exist) | (not exist) | Can be accessed from the class in the specified package + derived class * Specify only the package to which it belongs (intermediate hierarchy is also possible) |
(not exist) |
private[package name] | (not exist) | (not exist) | Accessable within the specified package * Specify only the package to which it belongs (intermediate hierarchy is also possible) |
(not exist) |
protected[name of the class] | (not exist) | (not exist) | Accessable from yourself or the specified class + derived class * Only classes outside of you can be specified |
(not exist) |
private[name of the class] | (not exist) | (not exist) | Accessable from yourself or a specified class * Only classes outside of you can be specified |
(not exist) |
protected[this] | (not exist) | (not exist) | Accessable from own instance + derived class | (not exist) |
private[this] | (not exist) | (not exist) | Only accessible from your instance | (not exist) |
Not specified | Accessable within the same package | Same as public | Same as public | Same as public |
Groovy is compatible with Java, so you can use Java-like access modifiers, but they don't work and you can access everything from anywhere (it's by default public). .. This behavior is managed as a bug, but it seems that it's still a long way from being fixed (verified by Groovy 2.4.12 in writing this article). Scala has a qualifier, which is characterized by fine-grained access control.
Alternative languages other than Java are common in that they become "public" when nothing is specified. "Do not write keywords for access control" ⇒ "No access control" ⇒ "Accessable from anywhere" I think it is intuitive and easy to understand.
[GROOVY-3010] fix private field visibility - ASF JIRA [GROOVY-7246] Access modifiers behave differently in Groovy when compared to Java. - ASF JIRA Scala class memo (Hishidama's Scala class Memo) Class member access modifiers | hydrocul notes First time in Kotlin. I compared it with Java
Recommended Posts