I am trying to restrict access to methods present in an object to the package the object belongs to. The method is used by many classes inside the package. I have two options:
protected[pkg] object MyObject{....}
or
private[pkg] object MyObject{....}
Both of them work just fine. My question is, since an object cannot be extended by any class/object anyway, aren't they equivalent?
Top-level, yes, they wind up as public in Java (not default access).
But also:
package accesstest {
trait T {
protected[accesstest] object Foo { def foo = 7 }
private[accesstest] object Bar { def bar = 8 }
}
object Test extends App {
val t = new T {}
Console println t.Foo.foo
Console println t.Bar.bar
Console println other.Other.foo
}
}
package other {
object Other extends accesstest.T {
def foo = Foo.foo
//def bar = Bar.bar // nope
}
}
So what counts is the extensibility and access of the enclosing thing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With