Reputation: 685
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
Reputation: 81
public class someclass<T> extends something<T> {
public something<T> somemethod() { ... return new someclass<T>(); }}
Upvotes: 0
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
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
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
Reputation: 6499
You also need to extend the interface with a generic, such as:
public class someclass implements something { public something somemethod() { ... } }
Upvotes: 0
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
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
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