scala - Accessing a protected member of a base class -


i'm not using inheritance often, i'm not sure why doesn't work. in project have following stuff:

base sealed class protected member:

sealed class theroot {   protected def some: string = "theroot" } 

and it's descendant logic:

final case class descendant() extends theroot {   def call: unit = {      val self: theroot =     self.some // <<- throw compilation error   } } 

compiling above gives me following error:

error: method in class theroot cannot accessed in theroot  access protected method not permitted because  prefix type theroot not conform  class descendant access take place            self.some 

i'm not sure what's problem call protected member super class... it's getting more interesting if wrap companion object, magically fixes problem:

sealed class theroot {   protected def some: string = "theroot" }  object theroot {   final case class descendant() extends theroot {     def call: unit = {       val self: theroot =       self.some // <<- no error!     }   } }   // exiting paste mode, interpreting.  defined class theroot defined object theroot 

as descripted in document

access protected members bit more restrictive in java. in scala, protected member accessible subclasses of class in member defined. in java such accesses possible other classes in same package. in scala, there way achieve effect, described below, protected free left is. example shown illustrates protected accesses:

 package p {   class super {     protected def f() { println("f") }   }   class sub extends super {     f()   }   class other {     (new super).f()  // error: f not accessible   } } 

in code, if change self.some some, ok.

while companion object can access members of companion class, descendant of object theroot can access protected method of some


Popular posts from this blog

php - How should I create my API for mobile applications (Needs Authentication) -

5 Reasons to Blog Anonymously (and 5 Reasons Not To)

Google AdWords and AdSense - A Dynamic Small Business Marketing Duo