Protected abstract vars in Scala can be implemented public?
Could someone explain why scala would allow a public variable, to satisfy
the implementation of an abstract declared Protected item? My first
assumption is that the compiler would complain, but I created a small test
to see if this worked, and to my surprise it does. Is there an advantage
to this? (perhaps this is normal in OOP?) Any methods to avoid the
accidental pitfall?
object NameConflict extends App {
abstract class A {
protected[this] var name:String
def speak = println(name)
}
class B(var name:String) extends A { //notice we've declared a public var
}
val t = new B("Tim")
t.speak
println(t.name) // name is exposed now?
}
No comments:
Post a Comment