Domanda

In java if you have an object that requires a generic to extends a particular class, but you want to implement it with an extension of that class is there any way to let java know that you are not actually breaking your own rules? Example code below in the assumption that that made no sense.

public interface WantedInterface extends StaticInterface<WantedObject>

public class WantedObject extends OlderObject

public class OlderObject extends UsefulObject

public interface StaticInterface<T extends UsefulObject>

So my question is essentially, is there any way for me to tell my StaticInterface that my WantedObject does in fact extend my UsefulObject even though it is not direct? I would really prefer not to have to rewrite my WantedObject as a pure extension of a UsefulObject just because the OlderObject contains a lot of useful code and I am writing to a database where the sequence I am using would want to have knowledge of the current state of OlderObject. I realized in writing this that I could just have WantedInterface directly extend the interface for OlderObject, but I want to know if there is a way to avoid this.

EDIT: Okay so, in a very weird twist of things I was able to fix the problem by changing the extension of WantedObject to UsefulObject, and then changing it back as it was. Perhaps the problem was just some temporary IDE thing? I'm using eclipse, does it have problems with this by chance?

È stato utile?

Soluzione

A extends B is transitive, which is a fancy way of saying that if A extends B and B extends C, then A extends C as well. For example, the compiler will have no problems with the following code:

UsefulObject uo = new WantedObject();
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top