question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

AbstractMethodError when inheriting trait from java abstract class and referencing val from the trait

See original GitHub issue

This is the exact same bug as in Scala 2: https://github.com/scala/bug/issues/12456

trait ScalaTrait {
  def foo1: String
  def foo2: String = "default foo value 2"

  val val1: String
  val val2: String = "default val value 2"
}
public abstract class JavaAbstractClass implements ScalaTrait {
    public String javaFoo1() { return foo1(); }
    public String javaFoo2() { return foo2(); }

    public String javaVal1() { return val1(); }
    public String javaVal2() { return val2(); }
}
import scala.util.Try

object ScalaClass extends JavaAbstractClass {
  override def foo1: String = "implemented foo in scala 1"
  override val val1: String = "implemented val in scala 1"

  def main(args: Array[String]): Unit = {
    println(Try(javaFoo1()))
    println(Try(javaFoo2()))

    println(Try(javaVal1()))
    println(Try(javaVal2()))
  }
}

problem

The program compiles fine, but an exception is thrown at runtime:

Success(implemented foo in scala 1)
Success(default foo value 2)
Success(implemented val in scala 1)
Exception in thread "main" java.lang.AbstractMethodError: Method ScalaClass$.val2()Ljava/lang/String; is abstract
	at ScalaClass$.val2(ScalaClass.scala)
	at JavaAbstractClass.javaVal2(JavaAbstractClass.java:6)
	at ScalaClass$.$anonfun$main$4(ScalaClass.scala:12)
	at scala.util.Try$.apply(Try.scala:210)
	at ScalaClass$.main(ScalaClass.scala:12)
	at ScalaClass.main(ScalaClass.scala)

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
smartercommented, Apr 17, 2022

and not create a setter method at all?

This doesn’t sound like a binary compatible change, so not something we can do in scala 3.

0reactions
anatoliykmetyukcommented, Apr 25, 2022

This issue was picked for the Issue Spree 15 of May 3rd which takes place a week from now. @gagandeepkalra and @ghostbuster91 will be working on it. If you have any insight into the issue or guidance on how to fix it, please leave it here.

Read more comments on GitHub >

github_iconTop Results From Across the Web

AbstractMethodError when inheriting trait from java abstract ...
NOTE: I have to use an intermediate java class in my Scala classes hierarchy to workaround some other compiler issues. The text was...
Read more >
scala - AbstractMethodError when mixing in trait nested in object
When I put the code in a file, compile it with scalac , and then use the compiled class files from the REPL...
Read more >
Scala trait examples and syntax | alvinalexander.com
You can use Scala traits like abstract classes in Java. In the following example, an implementation is provided for the speak method in...
Read more >
Abstract Type Members | Tour of Scala
Abstract types, such as traits and abstract classes, can in turn have abstract type members. ... trait Buffer: type T val element: T....
Read more >
Scala Trait and Abstract Class - Commit Logs
In Scala, abstract class and trait can both be used as interface to define properties for extending classes to implement.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found