This is a bit complicated. This is a mistake - the Scala compiler should throw an error - but it's a little subtle.
The problem is that since you left public
from the abstract method, its visibility is private to the package. If you write this in Java:
package javapkgimpl; public class JavaClassImpl extends javapkg.JavaClass { String foo(int b) { return (new java.lang.Integer(b)).toString(); } public static void main(String[] args) { System.out.println(new JavaClassImpl().bar(10)); } }
then the Java compiler will complain:
JavaClassImpl.java:3: javapkgimpl.JavaClassImpl is not abstract and does not override abstract method foo(int) in javapkg.JavaClass public class JavaClassImpl extends javapkg.JavaClass { ^ 1 error
It is explicitly trying to override, but actually it does not work, since it is not in the correct package and therefore cannot actually (or override) the original foo
. If we change the package to javapkg
, then everything will work. If we try this Scala instead:
package javapkg class ScalaClass extends JavaClass{ def foo(a:Int) = a.toString } object ScalaClass extends App{ println(new ScalaClass().bar(10)) }
then Scala works great.
The Scala compiler’s error is that it doesn’t notice this error - it should emit the same error as the Java compiler (or more useful, which actually tells you that access is disgusting!), But rather than emitting bytes code that cannot work.
But this is easy to fix: just change the package to Scala code (or access modifier in Java code).
source share