Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Generics - Type parameter `K` is not within its bound; should implement X

Tags:

java

generics

I am reading the java docs on generics, specifically this page on bounded types and am lost. Here is a simple version of my code, for context I generically want a compare-able key, one with an Integer and another abstract class with a Date, here is just the base abstract class and the integer implementation:

Abstract class:

public abstract class A<K extends Comparable<K>, V> extends TreeMap<K, V>

Integer Abstract class:

public abstract class B<K extends Integer, V> extends A<K,V>

The error I get is in the type reference of the second class at the part extends A<K,V>. Specifically, my IDE is underlying the K and says Type parameter 'K' is not within its bound; should implement 'java.lang.Comparable<K>'

I am confused on this because in the definition of Integer, it implements a Comparable<Integer>, is that not enough to satisfy this? Moreover, when I try to do the following, public abstract class B<K extends Integer & Comparable<K>, V> extends A<K,V>, that does not work either.

Am I misunderstanding something with generics?

like image 623
Steven K Avatar asked May 16 '19 06:05

Steven K


People also ask

How do you declare a generic bounded type parameter in Java?

To declare a bounded type parameter, list the type parameter's name, followed by the extends keyword, followed by its upper bound, which in this example is Number .

What are bounded type parameters in generic?

Whenever you want to restrict the type parameter to subtypes of a particular class you can use the bounded type parameter. If you just specify a type (class) as bounded parameter, only sub types of that particular class are accepted by the current generic class. These are known as bounded-types in generics in Java.

What is generic type parameter in Java?

Generic Methods A type parameter, also known as a type variable, is an identifier that specifies a generic type name. The type parameters can be used to declare the return type and act as placeholders for the types of the arguments passed to the generic method, which are known as actual type arguments.

How do you declare generics in Java?

A Generic Version of the Box Class To update the Box class to use generics, you create a generic type declaration by changing the code "public class Box" to "public class Box<T>". This introduces the type variable, T, that can be used anywhere inside the class.


2 Answers

Since Integer is a final class, the correct definition of B would be:

public abstract class B<V> extends A<Integer,V> {

}

You don't need the K type parameter.

like image 159
Eran Avatar answered Sep 21 '22 00:09

Eran


The compiler error occurs because although Date implements Comparable<Date>, a subclass of Date might not implement Comparable<SubclassOfDate>, so the compiler complains.

You can fix this by changing A's declaration to use a wildcard:

public abstract class A<K extends Comparable<? super K>, V> extends TreeMap<K, V>

However, you probably intended to declare B like this:

public abstract class B<V> extends A<Integer,V> {

}

Integer is final, no other class can inherit it. So B does not have to be generic on K.

As for the class with Date as the key, you probably want to do it like this as well:

public abstract class C<V> extends A<Date,V> {

}

But since Date is not final, you could make a generic parameter K extends Date:

public abstract class C<K extends Date, V> extends A<K,V> {

}
like image 33
Sweeper Avatar answered Sep 21 '22 00:09

Sweeper