Implementing Layered Java Interfaces in Scala

I have the following hierarchy in java for my interface

 public interface Identifiable<T extends Comparable<T>> extends Serializable { public T getId(); } public interface Function extends Identifiable { public String getId(); } public abstract class Adapter implements Function { public abstract String getId(); } 

When I try to implement Adapter in scala as follows

 class MultiGetFunction extends Adapter { def getId() : String = this.getClass.getName } 

I get the following error

 Multiple markers at this line - overriding method getId in trait Identifiable of type ()T; method getId has incompatible type - overrides Adapter.getId - implements Function.getId - implements Identifiable.getId 
+6
source share
1 answer

In general, this is a pain working with raw types in java code from Scala.

Try the following:

 public interface Function extends Identifiable<String> { public String getId(); } 

The error is probably due to the inability of the compiler to determine the type of T , since the type is not mentioned in the declaration of Function extends Identifiable . This is due to an error:

: 17: error: getId override method in the attribute Identifies type () T; getId method has incompatible type

Scala is made compatible with Java 1.5 and later. For previous versions you need to hack. If you cannot change the Adapter , you can create a Scala shell in Java:

 public abstract class ScalaAdapter extends Adapter { @Override public String getId() { // TODO Auto-generated method stub return getScalaId(); } public abstract String getScalaId(); } 

And then use this in Scala:

 scala> class Multi extends ScalaAdapter { | def getScalaId():String = "!2" | } defined class Multi 
+16
source

All Articles