I want to reference this
class type in generic interface.
I need this to enforce interface implementations to implement two methods which return implementations of generic classes with this
class type as generic parameter.
Let's assume I have two interfaces: Cover.java
and Adaptor.java
interface Cover<P extends Phone> {
P getPhone();
}
interface Adaptor<P extends Phone> {
P getPhone();
}
I want to create generic interface, that encourage it's implementations to return Cover
and Adaptor
with itself as generic parameter
// <T> should reference to current Phone implementation
public interface Phone {
Cover<T> getCover();
Adaptor<T> getAdaptor();
}
As a result I want:
//correct
public class Iphone implements Phone {
Cover<Iphone> getCover(); // <- I want this to be compile-time controlled
Adaptor<Iphone> getAdaptor(); // <- I want this to be compile-time controlled
}
// compile-time exception
public class Samsung implements Phone {
Cover<Iphone> getCover(); // <- CTE
Adaptor<Iphone> getAdaptor(); // <- CTE
}
Is there any way to achieve this in Java 7?