I got a strange error, here is a mininal REPL example:
scala> abstract class A[T] {protected def m: T}; class A1[T](a: A[T]) extends A[T] {def m = a.m}
<console>:9: error: method m in class A cannot be accessed in A[T]
Access to protected method m not permitted because
prefix type A[T] does not conform to
class A1 in object $iw where the access take place
abstract class A[T] {protected def m: T}; class A1[T](a: A[T]) extends A[T] {def m = a.m}
^
The same error also occurs when using compiler.
If ´m´ is declared public in the parent type, then everything is OK. But I should be able to access
protected members in a subclass, no?
May be, I'm missing something obvious?
Thank you,
EL
yes, with a parameter of the same type it works, of course.
But, for my purposes, it must be the supertype.
Here is yet another, even more simple example with the same error:
scala> :paste
// Entering paste mode (ctrl-D to finish)
class A {
protected def m {}
}
class B extends A {
protected def n(a: A) {a.m}
}
// Exiting paste mode, now interpreting.
<console>:12: error: method m in class A cannot be accessed in A
Access to protected method m not permitted because
prefix type A does not conform to
class B where the access take place
protected def n(a: A) {a.m}
^
I also found this (closed - not a bug) ticket https://issues.scala-lang.org/browse/SI-1383
and a short thread ´Bug in implementation of "protected"?´
http://comments.gmane.org/gmane.comp.lang.scala.user/8247 (with an example much like my example from
the prev. post).
In that thread, Martin says """
No, this is as intended. If you doubt it, compile the same example
with javac -- you should get the same result. This is a
little-understood subtlety in the meaning of protected: you can only
access a protected member from something that's as least as
specialized as your class. So in C you can access protected A-members
of other C objects, but not the same members of A objects.
"""
Here is a Java equivalent of the above Scala code:
class A {
protected void m() {}
}
class B extends A {
protected void n(A a) {a.m();}
}
It compiles *without* errors. (JDK 1.6.30)
So, the meaning of ´protected´ seems to be different in Scala and Java.
Is this really inteded?
Kind regards,
EL