tommy_p1ckles
tommy_p1ckles

Reputation: 685

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?

Upvotes: 3

Views: 7488

Answers (8)

Skazi_odc
Skazi_odc

Reputation: 81

public class someclass<T> extends something<T> {

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

Upvotes: 0

nazlo
nazlo

Reputation: 431

As I understand, you need to implement interface something, You can do this in simple way:

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

Upvotes: 0

Slawomir Szor
Slawomir Szor

Reputation: 343

To override a method you have to implement a method with the same types of paramenters and the same type of return value.
String something(){}
will be overriden by String something(){}
but NOT with char[] something(){} or String something(int){}

Upvotes: 0

Jesper
Jesper

Reputation: 206806

First of all, your someclass does not implement the interface in your code example above. You can make it implement the interface for a specific, concrete type, as in the following example, where String is used as the concrete type. The method then would need to return a something<String>.

public class someclass implements something<String> {
    public something<String> somemethod() {
        return new someclass();
    }
}

Or you could have class someclass have a type parameter and use that for the interface:

public class someclass<X> implements something<X> {
    public something<X> somemethod() {
        return new someclass<X>();
    }
}

Upvotes: 1

Michael
Michael

Reputation: 6499

You also need to extend the interface with a generic, such as:

public class someclass implements something { public something somemethod() { ... } }

Upvotes: 0

Vlad
Vlad

Reputation: 18633

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();
    }
}

Upvotes: 3

Maur&#237;cio Linhares
Maur&#237;cio Linhares

Reputation: 40333

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;
    }

}

Upvotes: 3

hvgotcodes
hvgotcodes

Reputation: 120188

from what i understand the abstract method somemethod() needs to overridden with a method that returns an object implementing the interface.

That is not correct. The method somemethod needs to return a something<T>.

Upvotes: 0

Related Questions