Scala access modifier

Scala access modifiers are basically the same as Java, which are: private, protected, public.

If no access modifier is specified, the access level of the Scala object is public by default.

The private qualifier in Scala is more restrictive than Java. In the case of nested classes, the outer class can't even access the private members of the nested class.

Private member

Modified with the private keyword, members with this tag are only visible inside the class or object that contains the member definition, and the same rules apply to inner classes.

class Outer{
    class Inner{
    private def f(){println("f")}
    class InnerMost{
        f() // correct
    (new Inner).f() //error

(new Inner).f( ) Access is illegal because f is declared private in Inner and access is not in class Inner.

But accessing f in InnerMost is fine, as this access is included in the Inner class.

Java allows these two types of access because it allows external classes to access private members of inner classes.

Protected members

In scala, access to Protected members is more restrictive than in Java. Because it only allows protected members to be accessed in subclasses of the class in which the member is defined. In java, members decorated with the protected keyword can be accessed by subclasses of the class that define the member, and other classes in the same package can also be accessed.

package p{
class Super{
    protected def f() {println("f")}
    class Sub extends Super{
    class Other{
        (new Super).f() //error

In the above example, the Sub class has no problem accessing f because f is declared protected in Super and Sub is a subclass of Super. In contrast, Other's access to f is not allowed because other does not inherit from Super. The latter is also recognized in java because Other is in the same package as Sub.

Public members

In Scala, if no modifier is specified, it defaults to public. Such members can be accessed from anywhere.

class Outer {
   class Inner {
      def f() { println("f") }
      class InnerMost {
         f() // correct
   (new Inner).f() // correct becausef() Yes public

Scope Protection

In Scala, access modifiers can be emphasized by using qualifiers. The format is:




Here x refers to a package, class, or singleton object to which it belongs. If written as private[x], read as "this member is private to all other classes except for the classes in [...] or the classes in the package [...] and their associated objects.

This technique is very useful in large projects that span several packages, allowing you to define things that are visible in several sub-packages of your project but are never visible to customers outside 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 contained in the bobsrockets package.

For example, access to the Navigator from the Vehicle object is allowed, because the object Vehicle is included in the package launch, and the launch package is in the bobsrockets. Instead, all the code outside the package bobsrockets is Cannot access the class Navigator.