0

I have the following Scala trait that I would like to inherit:

trait StreamTableSink[T] extends TableSink[T] {

  /** Emits the DataStream. */
  def emitDataStream(dataStream: DataStream[T]): Unit
}

that extends:

trait TableSink[T] {

  private var fieldNames: Option[Array[String]] = None
  private var fieldTypes: Option[Array[TypeInformation[_]]] = None
  ...
}

But when I inherit it in Java like this:

public abstract class KafkaTableSink implements StreamTableSink<Row> {
  ...
}

I receive the following error:

Error:(29, 8) java: kafka.KafkaAvroTableSink09 is not abstract and does not override abstract method TableSink$$fieldTypes_$eq(scala.Option<typeinfo.TypeInformation<?>[]>) in sinks.TableSink

where KafkaAvroTableSink09 inherits KafkaTableSink

6
  • You could just make fieldNames and fieldTypes vals' instead of vars'. Commented Jul 6, 2016 at 7:06
  • That question is talking about defs, not vars, but the problem and the solution are the same. Commented Jul 6, 2016 at 17:13
  • @AlexeyRomanov I don't think that is the duplicate of the question that you mentioned. I don't have any issues with the "def" and I successfully implemented the abstract method. I have an issue with the fieldTypes_$eq method which seems related to "fieldTypes" var to me. Commented Jul 6, 2016 at 21:45
  • vars create two non-abstract methods, so you have a trait with non-abstract methods, exactly as in the linked question. Same will happen with non-abstract vals. Commented Jul 7, 2016 at 6:30
  • @AlexeyRomanov Does it mean that it's impossible to inherit traits with "var"s? Commented Jul 7, 2016 at 8:09

1 Answer 1

1

Two problems might be arising.

1) java does not know what type to give fieldTypes due to the use of a placeholder type (underscore). This might lead to the generated fieldTypes_$eq method being made abstract (see here to see what java code is generated for a var)

2) scala traits cannot be extended in Java if they have any implementation details.

1 should be fixed by giving it an explicit type and 2 can be fixed by wrapping the trait in a class to extend in Java. (although 2 doesn't seem to be your problem, just something to be aware of)

Sign up to request clarification or add additional context in comments.

1 Comment

No, the problem is 2. _ in Scala is correctly translated into Java's ? wildcard, which you can see in the error message.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.