Java – Rewriting interface methods

For interfaces such as

public interface something<T>
{
   public something<T> somemethod();
}

From my understanding, the abstract method somemethod () needs to be overridden by a method that returns the object implementing the interface However, any attempt to do so gave me a compiler error that "will not overwrite the abstract method somemethod()"

I've tried to do something like that

public class someclass {
...
    public something<T> somemethod() { ... return new someclass(); }
...
or 
    public someclass somemethod() { ... return new someclass(); }
...
}

How on earth would I implement such an approach?

Solution

You are missing a generic declaration in the implementation class Here is an example:

public interface Something <T> {

    public Something<T> someMethod();

}

class SomethingImplementation <T> implements Something <T> {

    @Override
    public Something<T> someMethod() {
        return null;
    }

}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>