You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm running into an error where I have a Java abstract class extending a Scala abstract class, then I'm anonymously instantiating the Java abstract class in Scala without implementing a member. Rather than receive a compiler message about unimplemented abstract methods, the program compiles and then errors at runtime.
[error] java.lang.AbstractMethodError: bug.Foo.bar()Ljava/lang/String;
[error] at bug.Foo.baz(Foo.java:7)
[error] at bug.Main$.delayedEndpoint$bug$Main$1(Main.scala:5)
[error] at bug.Main$delayedInit$body.apply(Main.scala:3)
A few notes on how the problem changes when the code is altered:
Changing AbstractFoo to a trait gives the same result as when it is an abstract class (of course, the java code needs to switch in implements for extends as well).
Renaming the java field bar to _bar causes the error message that I would hope and expect to see:
.../src/main/scala/Main.scala:4:15: object creation impossible, since method bar in class AbstractFoo of type => String is not defined
Making the java field bar private also causes the same compiler error message.
Changing Main to call foo.bar instead of foo.baz causes the program to compile and run, printing "abc". I'm not certain whether this behavior is expected. It means that java fields named the same as scala methods receive precedence over the scala methods.
Changing the assignment in main to val f: AbstractFoo = new Foo() { } and the statement to println(f.bar) also compiles without error and causes an AbstractMethodError at runtime.
Environment information:
Tested under JDK 8u141 and JDK 9
Tested using sbt and scala versions 2.12.2 and 2.12.3
The text was updated successfully, but these errors were encountered:
mrerrormessage
changed the title
Scala abstract class / Java abstract class interaction compiles but throws AbstractMethodError at runtime
Scala trait / abstract class and Java abstract class interaction compiles but throws AbstractMethodError at runtime
Sep 29, 2017
This (and the mentioned variants) no longer compiles, I believe it was fixed by scala/scala#9525.
Test.scala:6: error: weaker access privileges in overriding
def bar: String (defined in class AbstractFoo)
override should be public
val f = new Foo() { }
^
1 error
I'm running into an error where I have a Java abstract class extending a Scala abstract class, then I'm anonymously instantiating the Java abstract class in Scala without implementing a member. Rather than receive a compiler message about unimplemented abstract methods, the program compiles and then errors at runtime.
The code here can be downloaded in this gist
I have in scala:
and in java:
And then back in scala:
Compiles without error, then when running, I see:
A few notes on how the problem changes when the code is altered:
AbstractFoo
to a trait gives the same result as when it is an abstract class (of course, the java code needs to switch inimplements
forextends
as well).bar
to_bar
causes the error message that I would hope and expect to see:bar
private also causes the same compiler error message.Main
to callfoo.bar
instead offoo.baz
causes the program to compile and run, printing "abc". I'm not certain whether this behavior is expected. It means that java fields named the same as scala methods receive precedence over the scala methods.val f: AbstractFoo = new Foo() { }
and the statement toprintln(f.bar)
also compiles without error and causes anAbstractMethodError
at runtime.Environment information:
The text was updated successfully, but these errors were encountered: