Scala access modifier


Release date:2023-11-11 Update date:2023-11-14 Editor:admin View counts:174

Label:

Scala access modifier

The Scala access modifier is basically the same as Java’s: private,protected,public.

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

In Scala private qualifiers, which are stricter than Java, in the case of nested classes, the outer class cannot even access the private members ofthe nested class.

Private member

Use private keyword modifier, the member with this tag is visible only within the class or object that contains the member definition, and the samerule applies to the inner class.

Example

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

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

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

These two kinds of access are allowed in Java because it allows external classes to access private members of inner classes.

Protected members

In scala, access to Protected members is more stringent than java. Because it only allows protected members to be accessed in subclasses of the class in which the member is defined. In java, use the protected keyword modified members can be accessed not only by subclasses of the class that defines the member, but also by other classes in the same package.

Example

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

In the above example, there is no problem with Sub class access to f because f is declared as protected 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 member

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

Example

class Outer {
   class Inner {
      def f() { println("f") }
      class InnerMost {
         f() // correct
      }
   }
   (new Inner).f() // Correct because f() is public
}

Scope protection

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

private[x]

or

protected[x]

The x here refers to a package, class, or singleton object to which it belongs. If written as private[x] read as “this member in addition to the […] , the class in or […] classes in the package and their concomitant objects are visible to all other classes private .

This technique is useful in large projects that span several packages, allowing you to define things that are visible in several subpackages of your project but are always invisible to customers outside the project.

Example

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] , that is, this class pair is contained in the bobsrockets all classes and objects in the package are visible.

For example, from Vehicle right in the object. Navigator access is allowed because the object Vehicle included in the package launch , and launch wrapped in bobsrockets on the contrary, allin the package bobsrockets none of the outside code can access the class Navigator .

Powered by TorCMS (https://github.com/bukun/TorCMS).