Access modifiers

Access modifiers can be augmented with qualifiers. A modifier of the form private[X] or protected[X] means that access is private or protected "up to" X, where X designates some enclosing package, class or singleton object.
package quizful {
   package prof {
      class Executive {
         private[prof] var details = null
         private[quizful] var friends = null
         private[this] var secrets = null

         def help(another : Executive) {
            println(another.details)
            println(another.secrets) //ERROR
         }
      }
   }
}
Note the following points:
  • Variable details will be accessible to any class within the enclosing package prof.
  • Variable friends will be accessible to any class within the enclosing package quizful.
  • Variable secrets will be accessible only on the implicit object within instance methods (this).

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Keep exploring
Scala snippets
Cosmo
Sign Up Now
or Subscribe for future quizzes