top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Limitation of Scala

+3 votes
160 views

Why does accessing a protected static inner class X of a superclass Y fail with "error: class X cannot be accessed in object Y"?

posted Jun 28, 2016 by Shivam Kumar Pandey

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote

This is a known limitation of Scala: there is no notion of 'static' members in Scala. Instead, Scala treats static members of class Y as members of the singleton object Y (the companion object of class Y). When inheriting from this class, one can access only protected members of class Y but cannot access protected members of object Y.
There's no way Scala can simulate static protected without impairing the integrity of Scala's object model in a fundamental way, so this is not going to change. To work around this limitation, one has to create an implementation of the enclosing class with Java code which encapsulates all accesses to the protected static inner class.

answer Jun 30, 2016 by Karthick.c
...