jaywalker
jaywalker

Reputation: 1146

Scala - Overriding a Java interface method in a Scala class

I have a Java interface Writer defined as following:

public interface Writer<K, V> {

    Iterator<Product2<K, V>> iterator ();
}

And i am trying to implement this interface in a Scala class ExternalWriter which is as following:

private class ExternalWriter[K, V, C]
  extends Logging
  with Writer[K, V] {

    override def iterator(): Iterator[Product2[K, C]] = {
        partitionedIterator.flatMap(pair => pair._2)
  }
}

But when I try to compile this code, I get an error:

Error: Overriding method iterator in trait SortShuffleFileWriter of type ()Iterator[Product2[K,V]]; method iterator has incompatible type override def iterator(): Iterator[Product2[K, C]] = {

How do I fix this?

Upvotes: 0

Views: 997

Answers (2)

Pratyush Das
Pratyush Das

Reputation: 514

Try replacing Iterator in your scala class with java.util.Iterator as the scala Iterator and the java Iterator are different.

private class ExternalWriter[K, V, C]
  extends Logging
  with Writer[K, V] {

    override def iterator(): java.util.Iterator[Product2[K, C]] = {
        partitionedIterator.flatMap(pair => pair._2)
  }
}

The above would be the modified code.

Upvotes: 0

Codebender
Codebender

Reputation: 14471

Why did you change V to C?

Your override method should be,

override def iterator(): Iterator[Product2[K, V]] = {
    partitionedIterator.flatMap(pair => pair._2)

If you want to use C, then you should implement Writer with C as,

with Writer[K, C] {

Upvotes: 2

Related Questions