views:

1352

answers:

4

I've got the following classes set up:

public abstract class Process<T,S> {
    ...
}

public abstract class Resource<T, S extends Process<T, S>> {
    protected S processer;
    ...
}

public class ProcessImpl<EventType1, EventType2> {
    ...
}

public class ResourceImpl extends Resource<EventType1, ProcessImpl> {
    processer = new ProcesserImpl();
    ...
}

Everything is fine until I get to the ResourceImpl. I'm told that ProcessImpl is not a valid substitute for the bounded parameter <S extends Process<T,S>> of the type Resource<T,S>.

I've tried various ways of getting around this and keep hitting a wall.

Does anyone have any ideas?

+7  A: 
public class ProcessImpl<EventType1, EventType2> {
...
}

Because ProcessImpl doesn't extend Process. Your ProcessImpl is not derived from Process, which is what you're declaring that parameter should be.

Shakedown
A: 

I can't see a way to edit the original version, or comment on given answers without a better rep.

This code will exist on a web layer, the eventtype2 is defined on the persistence layer and accessible only in the core layer which exists below this level.

So unfortunately without having a tight coupling, which I would like to avoid, I don't have access to EventType2.

I don't understand. Are you not able to make ProcessImpl extend from Process?
Shakedown
A: 

You might want to do something like this:

public abstract class Process<T, S> {
}

public abstract class Resource<T, S extends Process<T, S>> {
    S processor;

}

public class ProcessImpl extends Process<EventType1, ProcessImpl> {
}

public class ResourceImpl extends Resource<EventType1, ProcessImpl> {

}

If you constrain the S parameter of the Resource to be a processor you also need to properly declare it on the ProcessImpl class. I don't know what EventType2 is but it should be implementing Process interface. I assumed you actually want to say ProcessImpl.

Toader Mihai Claudiu
A: 

If you don't want your code to depend on some existing package, which contains the Process, you could also introduce some new interface package depending on nothing in the very bottom of the class hierarchy. (If you are able to change the constrains of the inheritance of course.)