Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 230
Core_Dumped Avatar asked Sep 05 '14 09:09

Core_Dumped


1 Answers

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.

like image 58
som-snytt Avatar answered Sep 22 '22 04:09

som-snytt