Java Generics, extended Generics and abstract classes

18,162

Solution 1

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.

Solution 2

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.

Share:
18,162
Admin
Author by

Admin

Updated on June 12, 2022

Comments

  • Admin
    Admin about 2 years

    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?