Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java generics: Bound mismatch: The type is not a valid substitute for the bounded parameter of the type

I am facing the following problem:

I have these class & interface definitions

public abstract class ViewModelRefreshPostListFragment<T extends IRefreshPostViewCallback, R extends RefreshPostViewModel<T>>
    extends RefreshPostListFragment implements IRefreshPostView {

    private final ViewModelHelper<T, R> mViewModeHelper = //error here
        new ViewModelHelper<>(); 

...
}

public abstract class RefreshPostViewModel<R1 extends IRefreshPostViewCallback> extends AbstractViewModel<IRefreshPostViewCallback> {}

public class ViewModelHelper<T extends IView, R extends AbstractViewModel<T>> {}

public abstract class AbstractViewModel<T extends IView> {}

public interface IRefreshPostViewCallback extends IView {}

Eclipse gives me still this error: Bound mismatch: The type R is not a valid substitute for the bounded parameter <R extends AbstractViewModel<T>> of the type ViewModelHelper<T,R>

Based on Java inheritance I created these 2 chains:

"Chain" from ViewModelRefreshPostListFragment class definition
1) R extends RefreshPostViewModel<T> -> R extends RefreshPostViewModel<R1 extends IRefreshPostViewCallback> -> R extends AbstractViewModel<IRefreshPostViewCallback>
1.1) T extends IRefreshPostViewCallback
1.2) T (from RefreshPostViewModel<T>) is replaced by <R1 extends IRefreshPostViewCallback> Consitent result from 1.1) and 1.2) so the T parameter should be OK.

"Chain" from ViewModelHelper class definition
2) R extends AbstractViewModel<T>
2.1) T extends IView, IRefreshPostViewCallback extends IView -> T can be replaced by IRefreshPostViewCallback

If I apply 2.1) on 1.1) && 1.2) we see, parameter T is consistent

From 1) follows R extends AbstractViewModel<IRefreshPostViewCallback> from 2) follows R extends AbstractViewModel<T> and from 2.1) follows that T can be replaced by IRefreshPostViewCallback, If I understand the things correctly, this error should not appear, could someone explain me, why is eclipse giving me the error ??

Thank you!

like image 446
FilipR Avatar asked Jun 04 '15 22:06

FilipR


People also ask

What is bounded type parameter in Java?

There may be times when you want to restrict the types that can be used as type arguments in a parameterized type. For example, a method that operates on numbers might only want to accept instances of Number or its subclasses. This is what bounded type parameters are for.

How to declare Java generic bounded type parameter?

For example, a method that operates on numbers might only want to accept instances of Number or its subclasses. This is what bounded type parameters are for. To declare a bounded type parameter, list the type parameter's name, followed by the extends keyword, followed by its upper bound.

What is bound mismatch in Java?

Bound mismatch: The type LivingThing is not a valid substitute for the bounded parameter <T extends Animal> of the type GenericClass<T>

Do generics prevent type cast errors?

Implementing generics into your code can greatly improve its overall quality by preventing unprecedented runtime errors involving data types and typecasting.


1 Answers

The error message comes from the fact that R is not within its bounds.

Your ViewModelHelper class extends AbstractViewModel<IRefreshPostViewCallback>, no matter what R1 really is.

In the class ViewModelHelper, change the type argument in the extends clause of AbstractViewModel to R1, instead of IRefreshPostViewCallback.

public abstract class RefreshPostViewModel<R1 extends IRefreshPostViewCallback>
    extends AbstractViewModel<R1>

and this will eliminate the error.

This will pass the proper T along in ViewModelHelper. Instead of R being RefreshPostViewModel<IRefreshPostViewCallback>, you will be using RefreshPostViewModel<T>, fulfilling the bounds.

like image 54
rgettman Avatar answered Sep 28 '22 01:09

rgettman