How would you change declaration of function foo to ensure that a function foo is only accessible by classes defined in the package com.quizful ?
package com.quizful

class Foo {
   def foo = {...}
}
Explanation
Get an explanation when it's available:
Theory
  • 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