- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
rules for protected access
Mon, 2011-12-12, 08:15
As far as I can tell, "protected" in Scala is more or less defaults to
"protected[this]" What is the access modifier I need to make it behave
like "protected" in Java (or C++)? In other words, I want access to
protected members on all instances of derived classes, not just "this"
but "that" too. I've googled it, but I can't seem to find the answer.
Thanks.
--Russ P.
Mon, 2011-12-12, 17:21
#2
Re: rules for protected access
Sorry, incomplete.
On Mon, Dec 12, 2011 at 10:00 AM, Nils Kilden-Pedersen <nilskp@gmail.com> wrote:
scala> new Bar3 66res2: Bar3 = Bar3@1bab2a6
scala> res2.hello(new Bar3)6 66 6
On Mon, Dec 12, 2011 at 10:00 AM, Nils Kilden-Pedersen <nilskp@gmail.com> wrote:
On Mon, Dec 12, 2011 at 1:13 AM, Russ P. <russ.paielli@gmail.com> wrote:scala> class Bar3 extends Bar2 { | def hello(b: Bar3) = println(this.foo + " " + b.foo) | } defined class Bar3As far as I can tell, "protected" in Scala is more or less defaults to
"protected[this]" What is the access modifier I need to make it behave
like "protected" in Java (or C++)? In other words, I want access to
protected members on all instances of derived classes, not just "this"
but "that" too. I've googled it, but I can't seem to find the answer.
Thanks.
Seems to behave as I would expect:
scala> trait Foo { | protected val foo = 6 | }defined trait Foo
scala> class Bar extends Foo { | println(foo) | }defined class Bar
scala> class Bar2 extends Bar { | println(foo) | }defined class Bar2
scala> new Bar266 res0: Bar2 = Bar2@3a2b3574
scala> res0.foo <console>:12: error: value foo in trait Foo$class cannot be accessed in Bar2 Access to protected value foo not permitted because enclosing class object $iw in object $iw is not a subclass of trait Foo in object $iw where target is defined res0.foo ^
scala> new Bar3 66res2: Bar3 = Bar3@1bab2a6
scala> res2.hello(new Bar3)6 66 6
Mon, 2011-12-12, 21:51
#3
Re: rules for protected access
Thanks. Yeah, I was misinterpreting what was going on in my code. I
didn't notice that the "that" that I couldn't get access into was not
the same class as the "this" but rather a superclass. But since the
"protected" modifier was in that superclass constructor, I would have
thought that it applied equally to combinations of the class and
superclass. Apparently not.
--Russ P.
On Dec 12, 8:04 am, Nils Kilden-Pedersen wrote:
> Sorry, incomplete.
>
> On Mon, Dec 12, 2011 at 10:00 AM, Nils Kilden-Pedersen wrote:
>
>
>
>
>
>
>
>
>
> > On Mon, Dec 12, 2011 at 1:13 AM, Russ P. wrote:
>
> >> As far as I can tell, "protected" in Scala is more or less defaults to
> >> "protected[this]" What is the access modifier I need to make it behave
> >> like "protected" in Java (or C++)? In other words, I want access to
> >> protected members on all instances of derived classes, not just "this"
> >> but "that" too. I've googled it, but I can't seem to find the answer.
> >> Thanks.
>
> > Seems to behave as I would expect:
>
> > scala> trait Foo {
> > | protected val foo = 6
> > | }
> > defined trait Foo
>
> > scala> class Bar extends Foo {
> > | println(foo)
> > | }
> > defined class Bar
>
> > scala> class Bar2 extends Bar {
> > | println(foo)
> > | }
> > defined class Bar2
>
> > scala> new Bar2
> > 6
> > 6
> > res0: Bar2 = Bar2@3a2b3574
>
> > scala> res0.foo
> > :12: error: value foo in trait Foo$class cannot be accessed in
> > Bar2
> > Access to protected value foo not permitted because
> > enclosing class object $iw in object $iw is not a subclass of
> > trait Foo in object $iw where target is defined
> > res0.foo
> > ^
>
> scala> class Bar3 extends Bar2 {
> | def hello(b: Bar3) = println(this.foo + " " + b.foo)
> | }
> defined class Bar3
>
> scala> new Bar3
> 6
> 6
> res2: Bar3 = Bar3@1bab2a6
>
> scala> res2.hello(new Bar3)
> 6
> 6
> 6 6
Seems to behave as I would expect:
scala> trait Foo { | protected val foo = 6 | }defined trait Foo
scala> class Bar extends Foo { | println(foo) | }defined class Bar
scala> class Bar2 extends Bar { | println(foo) | }defined class Bar2
scala> new Bar266 res0: Bar2 = Bar2@3a2b3574
scala> res0.foo <console>:12: error: value foo in trait Foo$class cannot be accessed in Bar2 Access to protected value foo not permitted because enclosing class object $iw in object $iw is not a subclass of trait Foo in object $iw where target is defined res0.foo ^