What do class / object / level access modifiers mean?

In scala, I can add access modifiers to the class / trait / object like

private class Foo
protected[this] trait Foo

      

I haven't found any good explanation for these class / property / object modifiers. Do all such combinations make sense and what do they actually mean?

+3


source to share


1 answer


They mean the same as access modifiers for class / attribute members, since classes and traits can also be members of other classes. For example:

class A {
    private class Foo
}

      

The class Foo

is available only to the class A

. If I change the modifier to private[this]

then it is called private and therefore any Foo

is only accessible to it by the parent instance A

.

Ads private

, private[this]

, protected

or protected[this]

only really makes sense within another class or traits, because it has to be private. In this case, it Foo

is closed to A

. The same goes for traits.



We also cannot have an object-object and make it private.

package com.example.foo

private[foo] class Foo

      

The class Foo

is now only visible to other members of the package com.example.foo

.

Do they make sense? In some cases, I'm sure it's useful to have private classes and traits inside some other object.

+3


source







All Articles