Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

overriding interface method

For a interface such as

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

from what i understand the abstract method somemethod() needs to overridden with a method that returns an object implementing the interface. However, any attempts to do so have given me the "does not override the abstract method somemethod()" compiler error.

I've tried doing something like

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

How exactly would i implement such a method?

like image 386
tommy_p1ckles Avatar asked Sep 04 '26 00:09

tommy_p1ckles


2 Answers

You're missing the generic declaration in your implementing class. Here's an example:

public interface Something <T> {

    public Something<T> someMethod();

}

class SomethingImplementation <T> implements Something <T> {

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

}
like image 100
Maurício Linhares Avatar answered Sep 05 '26 14:09

Maurício Linhares


All of these should compile:

class test<T> implements something<T>{
    public something<T> somemethod(){
        return new test<T>();
    }
}

class test1<T> implements something<T>{
    public test1<T> somemethod(){
        return new test1<T>();
    }
}   

class test2 implements something<Integer>{
    public something<Integer> somemethod(){
        return new test2();
    }
}

class test3 implements something<Integer>{
    public test3 somemethod(){
        return new test3();
    }
}
like image 30
Vlad Avatar answered Sep 05 '26 15:09

Vlad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!