English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Scala's access modifiers are basically the same as those in Java, including: private, protected, public.
If no access modifier is specified, by default, the access level of Scala objects are all public.
The private qualifier in Scala is more strict than in Java; in the case of nested classes, the outer class cannot even access the private members of the nested class.
Members modified with the private keyword are only visible within the class or object that contains the member definition, and the same rule applies to inner classes.
class Outer{ class Inner{ private def f(){ println("f") } class InnerMost{ f() // Correct } } (new Inner).f() //Incorrect }
The access (new Inner).f() is illegal because f declared as private in Inner, and the access is not within the class Inner.
but accessing it in InnerMost f There is no problem with this access because it is included within the Inner class.
Java allows both accesses because it allows external classes to access the private members of inner classes.
In Scala, access to protected members is more strict than in Java. This is because it only allows protected members to be accessed in subclasses of the class where the member is defined. In Java, members marked with the protected keyword, in addition to being accessible by subclasses of the defining class, can also be accessed by other classes in the same package.
package p{ class Super{ protected def f() { println("f") } } class Sub extends Super{ f() } class Other{ (new Super).f() //Incorrect } }
In the example, there is no problem with Sub class accessing f because f is declared as protected in Super, and Sub is a subclass of Super. Conversely, Other's access to f is not allowed because Other does not inherit from Super. The latter is also recognized in Java because Other and Sub are in the same package.
In Scala, if no modifier is specified, it defaults to public. Such members can be accessed anywhere.
class Outer { class Inner { def f() { println("f") } class InnerMost { f() // Correct } } (new Inner).f() // Correct because f() is public }
In Scala, access modifiers can be emphasized by using qualifiers. The format is:
private[x] or protected[x]
Here, x represents a package, class, or singleton object. If written as private[x], it is read as "This member is private to the class in [...] or the class in [...] of the package and their companion objects, and is private to all other classes."
This technique is very useful in large projects spanning several packages, allowing you to define some things that are visible in several sub-packages of your project but always invisible to external customers of the project.
package bobsrockets{ package navigation{ private[bobsrockets] class Navigator{ protected[navigation] def useStarChart(){} class LegOfJourney{ private[Navigator] val distance = 100 } private[this] var speed = 200 } } package launch{ import navigation._ object Vehicle{ private[launch] val guide = new Navigator } } }
In the above example, the class Navigator is marked as private[bobsrockets], which means that this class is visible to all classes and objects included in the bobsrockets package.
For example, access to Navigator from the Vehicle object is allowed because the object Vehicle is included in the package launch, which is included in bobsrockets. Conversely, all code outside the package bobsrockets cannot access the class Navigator.