Scala access modifiers

Members of a package, class or object can be marked with the access modifiers private and protected, if we do not use these two keywords, then the access will be set to public by default. These decorations restrict access to certain regions of the member's code. To use access modifiers, include keywords defined by members of its package, class or object, as we will see in the next section.
Private members:

Private members can only see the class or object defined by the members contained inside. Here is an example:
class Outer {
   class Inner {
      private def f() { println("f") }
      class InnerMost {
         f() // OK
      }
   }
   (new Inner).f() // Error: f is not accessible
}
In Scala, access (new Inner).f() is illegal because f is declared as a private inner class and the access is not inside the inner class. In contrast, access to the innermost layer of the first class is determined because the access is contained within the body of the class. Java will allow both kinds of access because it gives the outer class of its inner class access to private members.
Protected members:

Protected members are only accessible from subclasses of the class in which the member is defined. Here is an example:
package p {
   class Super {
      protected def f() { println("f") }
   }
   class Sub extends Super {
      f()
   }
   class Other {
     (new Super).f() // Error: f is not accessible
   }
}
It is normal for a class to be assigned access to f because f is declared as a protected superclass and A subclass is a subclass of super. By contrast, accessing f in other is not allowed, because other does not inherit from super. In Java, the latter access will still be allowed, since the others are in the same package.
Public Members:

Every member that is not marked private or protected is public. There is no need to explicitly use the modifier public. Such members can be accessed from anywhere. Here is an example:
class Outer {
   class Inner {
      def f() { println("f") }
      class InnerMost {
         f() // OK
      }
   }
   (new Inner).f() // OK because now f() is public
}
protected scope:

Access modifiers in Scala can be augmented with use modifiers. Modifiers of the form: private[X] or protected[X] mean access to private or protected "up to" X, where X represents some enclosing package, class or single object. Consider the following example:
package society {
   package professional {
      class Executive {
         private[professional] var workDetails = null
         private[society] var friends = null
         private[this] var secrets = null

         def help(another : Executive) {
            println(another.workDetails)
            println(another .secrets) //ERROR
         }
      }
   }
}
Note the following in the above example: the
variable workDetails will be available for any class within the professional scope of the enclosing package.
The variable friends will be available to any class in the enclosing package society.
The variable secrets will only be available to the implicit object (this) in instance methods.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326529653&siteId=291194637