1

How do you implement a Java abstract interface in scala?

Abstract interface :

public abstract interface KeyIndex<K>
  extends Serializable
{
  public abstract long toIndex(K paramK);

  public abstract Seq<Tuple2<Object, Object>> indexRanges(Tuple2<K, K> paramTuple2);
}
2
  • 1
    It seems you are attempting to convert Java to Scala? The closest equivalent to an interface is a trait - give that a try. Commented Mar 9, 2016 at 6:05
  • 1
    You can leave out the abstract keyword from your interface and method declarations, an interface and its methods are always abstract. Commented Mar 9, 2016 at 10:40

1 Answer 1

1

It seems, that your KeyIndex class is written in Java. In Scala every field not labeled private or protected is public. There is no public keyword in Scala. But implementing a Java class in Scala is possible:

class KeyIndexImpl extends KeyIndex[geotrellis.spark.SpatialKey]{
  override def toIndex(paramK: geotrellis.spark.SpatialKey): Long = 
    1l

  override def indexRanges(paramTuple2: (geotrellis.spark.SpatialKey, geotrellis.spark.SpatialKey)): Seq[(AnyRef, AnyRef)] = 
    Seq((paramTuple2, paramTuple2))
}
Sign up to request clarification or add additional context in comments.

7 Comments

thank you. When i implement toIndex , i get the output of type "long" . from the error, type mismatch; found : Long required: geotrellis.spark.io.index.KeyIndex[geotrellis.spark.SpatialKey] , i should get an output of type geotrellis.spark.io.index.KeyIndex
yes in my example i used Int as the Type Parameter, you have to use geotrellis.spark.SpatialKey. i edited my answer for you
i get compilation errors by doing the above way. [ERROR] C:\Users\prasanna.s\workspace\RasterDataAnalysis\src\main\scala\RasterDataPOC\RasterDataAnalysis\ReadTiff.scala:24: error: overriding method toIndex in trait KeyIndex of type (key: geotrellis.spark.SpatialKey)Long; method toIndex has incompatible type [ERROR] override def toIndex(paramK: SpatialKey) : KeyIndex[SpatialKey] ^ [ERROR] C:\Users\prasanna.s\workspace\RasterDataAnalysis\src\main\scala\RasterDataPOC\RasterDataAnalysis\ReadTiff.scala:23: error: class KeyIndexImpl needs to be abstract, since:
override def toIndex(paramK: SpatialKey) : KeyIndex[SpatialKey] should be override def toIndex(paramK: SpatialKey): Long, the error is saying, that the return type of the implementation of toIndex not matches the definition in the base class (java)
I tried that , but the return type becomes long instead of SpatialKey -- type mismatch; found : Long required: geotrellis.spark.io.index.KeyIndex[geotrellis.spark.SpatialKey]
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.