Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Scala Access Modifiers

Scala Access Modifiers

In Scala, access modifiers control the visibility and access rights of classes, objects, traits, and their members (such as fields and methods).

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

If no access modifier is specified, Scala objects are public by default.

The private qualifier in Scala is stricter than in Java: with nested classes, the outer class cannot even access the private members of the nested class.


Private Members

Marked with the private keyword, such members are visible only inside the class or object that contains their definition; the same rule applies to inner classes.

Example

class

Outer

{

   

class

Inner

{

       

private

def

f

(

)

{

            println

(

"f"

)

       

}

       

class

InnerMost

{

            f

(

)

// Correct

       

}

   

}

   

(

new

Inner

)

.

f

(

)

// Error

}

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

But accessing it inside InnerMost f is fine, because this access is contained within class Inner.

Java allows both kinds of access because it permits an outer class to access an inner class's private members.


Protected Members

In Scala, access to protected members is stricter than in Java. It allows a protected member to be accessed only in subclasses of the class that defines it. In Java, a member marked protected can be accessed not only by subclasses of the defining class 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 example above, Sub's access to f is fine because f is declared protected in Super and Sub is a subclass of Super. By contrast, Other's access to f is not allowed because Other does not inherit from Super. The latter is allowed in Java, however, because Other and Sub are in the same package.


Public Members

In Scala, if no modifier is specified, the default is 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

}


Package-Private Access

Scala also supports a package-private access modifier, used to restrict a member's access to a specific package. It is written by following the private keyword with a package name. The format is:



private[x] 



或 



protected[x]

Here x refers to some enclosing package, class, or singleton object.

If written as private[x], it reads "this member is private to all other classes, except classes in […] or classes in the package […] and their companion objects"。

This technique is very useful in large projects spanning several packages; it lets you define things that are visible in several subpackages of your project but always invisible to clients 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 example above, the class Navigator is marked private[bobsrockets], meaning it is visible to all classes and objects contained in the bobsrockets package.

For example, accessing Navigator from the Vehicle object is allowed because Vehicle is contained in the launch package, and launch is inside bobsrockets; conversely, all code outside the bobsrockets package cannot access class Navigator.


Companion Objects and Companion Classes

In Scala, a companion object and its companion class can access each other's private members. A companion object and companion class are a class and an object with the same name in the same file.

Example

class

CompanionExample

{

 

private

val

privateField

:

String

=

"I am private in class"

}

object

CompanionExample

{

 

def

accessPrivateField

(

example

:

CompanionExample

)

:

String

=

{

    example.

privateField

 

// A companion object can access a class's private members

 

}

}

object

Main

{

 

def

main

(

args

:

Array

[

String

]

)

:

Unit

=

{

   

val

example

=

new

CompanionExample

    println

(

CompanionExample.

accessPrivateField

(

example

)

)

 

}

}