Ihave some codes written in Java. And for new classes I plan to write in Scala. I have a problem regarding accessing the protected static member of the base class. Here is the sample code:
Java code:
class Base{
protected static int count = 20;
}
scala code:
class Derived extends Base{
println(count);
}
Any suggestion on this? How could I solve this without modifying the existing base class
This isn’t possible in Scala. Since Scala has no notation of
staticyou can’t accessprotected staticmembers of a parent class. This is a known limitation.The work-around is to do something like this:
Now you can inherit from this new class instead and access the static member through the getter/setter methods:
It’s ugly—but if you really want to use
protected staticmembers then that’s what you’ll have to do.