Reputation: 1623
In scala I can add access modifiers to a class/trait/object, like
private class Foo
protected[this] trait Foo
I have not found any good explanation on these class/trait/object-level modifiers. Do all such combinations make sense and what do they actually mean?
Upvotes: 3
Views: 472
Reputation: 55569
They mean the same as access modifiers for class/trait members, as classes and traits can also be members of other classes. For example:
class A {
private class Foo
}
Class Foo
is only visible to class A
. If I change the modifier to private[this]
, then it is called object private, and so any Foo
is only visible to it's parent instance of A
.
Declaring private
, private[this]
, protected
or protected[this]
only really makes sense within another class or trait, because it has to be private to something. In this case, Foo
being private to A
. The same applies to traits.
We could also not have a containing object, and make them package private.
package com.example.foo
private[foo] class Foo
Now class Foo
is only visible to other members of the package com.example.foo
.
Do they make sense? In some cases I'm sure it is useful to have private classes and traits within some other object.
Upvotes: 3