6

I have 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 am getting 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
1

1 Answer 1

16

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

Try the below:

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

The error is probably due to inability of compiler to determine the type T as no type is mentioned when declaring Function extends Identifiable. This is explained from error:

:17: error: overriding method getId in trait Identifiable of type ()T; method getId has incompatible type

Scala is made to be compatible with Java 1.5 and greater. For the previous versions, you need to hack around. If you cannot change Adapter, then you can create a Scala wrapper 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
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Jatin, But I cannot change Java Definition and my Java version is 1.6
@Avinash Updated the code. This should make it work.

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.