Java Generics, Extended Generic and Abstract Classes

I have the following classes installed:

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 ResourceImpl. I was told that ProcessImplit is not a valid replacement for a limited <S extends Process<T,S>>type parameter Resource<T,S>.

I tried various ways to get around this and keep hitting the wall.

Does anyone have any ideas?

+5
source share
4 answers
public class ProcessImpl<EventType1, EventType2> {
...
}

Because ProcessImpl does not perform the extension . Your ProcessImpl is not derived from Process, this is what you declare with this parameter.

+11
source

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> {

}

S Resource , ProcessImpl. , EventType2, Process. , ProcessImpl.

+1

.

-, eventtype2 , .

, , , , EventType2.

0

If you do not want your code to depend on any existing package that contains Process, you can also introduce some new interface package depending on nothing at the very bottom of the class hierarchy. (Unless, of course, you can change the inheritance restrictions.)

0
source

All Articles