Difference between protected [package] and private [package] for a singleton object

I am trying to restrict access to the methods present in the object, to the package to which the object belongs. 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 perfectly. My question is that since an object cannot be extended by any class / object in any case, are they equivalent?

+4
source share
1 answer

The top level, yes, they end up being publicly available 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, all that matters is extensibility and access to closing things.

+1

All Articles