|
Answer» Scala defines 3 types of access modifiers: Default: This is equivalent to the publicin Java, i.e., accessible from anywhere. Protected: This makes members accessible in class, companion classes, and subclasses. However, one can mention the scope within which it can be accessible. Private: The members with this access are available only in the same class and in the companion class - not in a subclass.
We can take an example to understand the scope of private and protected: package amt {
class A(protected[amt] val a:Int, private[this] val b:Int) package amt.sub1 {
class B {
val x = NEW A(10, 20) val p = x.a
val q = x.b // error: not accessible
}
}
} In the above code, as the protected scope is bound inside package amt, the variable is accessible inside B. On the other hand, the scope of private is MENTIONED like this, that’s why it is not accessible OUTSIDE its instance.
|