Scala access modifiers


Scala access modifiers are basically the same as Java's, including: private, protected, and public.

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

The private qualifier in Scala is more strict than Java. In the case of nested classes, the outer class cannot even access the private members of the nested class.


Private members

are modified with the private keyword. Members with this mark are only visible inside the class or object that contains the member definition. The same rules also apply Inner class.

class Outer{
    class Inner{
    private def f(){println("f")}
    class InnerMost{
        f() // 正确
        }
    }
    (new Inner).f() //错误
}

(new Inner).f() The access is illegal because f is declared as private in Inner, and the access is not within the class Inner.

But there is no problem when accessing f in InnerMost, because this access is included in the Inner class.

Both types of access are allowed in Java because it allows outer classes to access private members of inner classes.


Protected members

In scala, access to protected members is more strict 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 modified with the protected keyword can be accessed not only by subclasses of the class that defines the member, but also 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() //错误
	}
}

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 and Sub are in the same package.


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() // 正确
      }
   }
   (new Inner).f() // 正确因为 f() 是 public
}

Scope protection

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

private[x] 

或 

protected[x]

The x here refers to a certain package, class or singleton object. If written as private[x], it is read as "This member is private to all other classes except the classes in [...] or the classes in the package in [...] and their companion objects.

This technique is very useful in large projects that span several packages. It allows you to define something that is visible in several sub-packages of your project but is always invisible to customers outside the project

package bobsrocckets{
    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, Navigator is visible from the Vehicle object. Access is allowed because the object Vehicle is contained in the package launch, and the launch package is in bobsrockets. On the contrary, all code outside the package bobsrockets cannot access the class Navigator

.