|
View:
New views
4 Messages
—
Rating Filter:
Alert me
|
|
|
Qualified private/protected modifier.Hello All,
I have a question regarding qualified modifiers in Scala. Could you please explain what is the behavior of private[C]/protected[C] in relation to inheritance? I think, I don't understand two sentences from section 5.2 of Scala Reference: (about private[C]) "Such members are also inherited only from templates inside C", and (about protected[C]) "r 's type conforms to a type-instance of the class which contains the access." Could you please elaborate about this or provide an example? I have found those modifiers very unintuitive; surprisingly, following program does compile. Could you please explain why? Thank you, Jarek ----- package pkg { package pkg2 { class A { private[pkg2] def f = 0 } object Main { def main(args: Array[String]) { val x: A = new B println(x.f) // outputs 1 !!! } } } class B extends pkg2.A { override def f = 1 // !!!! Why can I override this?? } } ------------------- Tanie rozmowy telefoniczne! Sprawdz >> http://link.interia.pl/f2410 |
|
|
Re: Qualified private/protected modifier.Private members can only be seen inside the scope of C. Protected members can be seen my anything inheriting from the scope of C.
I have no clue why the override in the end compiles. Might even be a bug.
At any rate, private, protected and public are visibility rules. Though the former two have by default their own class, they can be extended to include any enclosing package.
And, by the way, there is also private[this], which is only visible from inside the same instance. Or, in other words, a method cannot see a private[this] of another instance of the same class, only of itself.
2009/11/3 Jaroslaw Rosiek <jaroslaw.rosiek@...> Hello All, -- Daniel C. Sobral Veni, vidi, veterni. |
|
|
|
|
|
Re: Qualified private/protected modifier.Is it? Then I stand corrected.
On Wed, Nov 4, 2009 at 7:00 AM, Stefan Langer <mailtolanger@...> wrote: Forgot to reply to all again :( -- Daniel C. Sobral Veni, vidi, veterni. |
| Free embeddable forum powered by Nabble | Forum Help |