Qualified private/protected modifier.

View: New views
4 Messages — Rating Filter:   Alert me  

Qualified private/protected modifier.

by Jarosław Rosiek :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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.

by Daniel Sobral :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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,

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




--
Daniel C. Sobral

Veni, vidi, veterni.

Parent Message unknown Fwd: Qualified private/protected modifier.

by Stefan Langer-4 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Forgot to reply to all again :(

---------- Forwarded message ----------
From: Stefan Langer <mailtolanger@...>
Date: 2009/11/4
Subject: Re: [scala-user] Qualified private/protected modifier.
To: Daniel Sobral <dcsobral@...>


Although Sobral is correct I think that in 2.7.6 (and probably 2.7.7 as well) private[package] and protected[package] result in the same visibility rule in relation to package. Not sure about 2.8 though.

2009/11/3 Daniel Sobral <dcsobral@...>

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,

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




--
Daniel C. Sobral

Veni, vidi, veterni.



Re: Qualified private/protected modifier.

by Daniel Sobral :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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 :(

---------- Forwarded message ----------
From: Stefan Langer <mailtolanger@...>
Date: 2009/11/4
Subject: Re: [scala-user] Qualified private/protected modifier.
To: Daniel Sobral <dcsobral@...>


Although Sobral is correct I think that in 2.7.6 (and probably 2.7.7 as well) private[package] and protected[package] result in the same visibility rule in relation to package. Not sure about 2.8 though.

2009/11/3 Daniel Sobral <dcsobral@...>

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,

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




--
Daniel C. Sobral

Veni, vidi, veterni.





--
Daniel C. Sobral

Veni, vidi, veterni.