I am using eclipse to learn Java generics and lambda basics and I encountered a problem with the implementation of functional interface. I wanted to implement the method via lambda expression, but then compiler constantly shows "The type MyGenericClass must implement the inherited abstract method FunctionalIfce.getType()" even though I implemented it via lambda expression and later on even used in a program. When running there are no problems - all of the results are correct but error persists. Everything is within one package. Here are the implementations:
import java.lang.reflect.Type;
@FunctionalInterface
public interface FunctionalIfce {
Type getType();
}
import java.lang.reflect.Type;
public class MyGenericClass<T extends Number>
implements FunctionalIfce{
private T value;
// Lambda implementation of interface
public FunctionalIfce fIfce = () -> {
Type parameterType = value.getClass();
return parameterType;
};
public T getValue() {
return value;
}
public void setValue(T value) {
this.value = value;
}
@Override
public String toString() {
return "The value is equal: "+value+" and it's type: "+fIfce.getType();
}
//Stadard implementation ### Lambda implementation above
/*
@Override
public Type getType() {
Type parameterType = value.getClass();
return parameterType;
}
*/
}
And from Main class I call it as followed:
public class AdvancedEntry {
public static void main(String[] args) {
MyGenericClass<Integer> generic = new MyGenericClass<>();
generic.setValue(5);
System.out.println(generic.toString());
}
}
Is that an eclipse problem or did I make a mistake in my implementation that magically works? I will really appreciate any help on this puzzle since I found nothing that would remotely look like my issue.
You need to remove the interface implementation and it works.
import java.lang.reflect.Type;
public class MyGenericClass<T extends Number>{
private T value;
// Lambda implementation of interface
public FunctionalIfce fIfce = () -> {
Type parameterType = value.getClass();
return parameterType;
};
public T getValue() {
return value;
}
public void setValue(T value) {
this.value = value;
}
@Override
public String toString() {
return "The value is equal: "+value+" and it's type: "+fIfce.getType();
}
//Stadard implementation ### Lambda implementation above
/*
@Override
public Type getType() {
Type parameterType = value.getClass();
return parameterType;
}
*/
}