+ 3

Generics code reuse

Hi, Does anyone know how i can reduce this line so I dont have to repeat extends Number twice? public class Eval<T extends Number ,V extends Number>implements Sums<T,V>{ Also does anyone have more information on the syntax of these types I would like to know all the different ways I can use them Thanks

11th Feb 2021, 12:46 AM
D_Stark
D_Stark - avatar
7 Respostas
+ 4
WILDCARDS In generic code, the question mark (?), called the wildcard, represents an unknown type. The wildcard can be used in a variety of situations: as the type of a parameter, field, or local variable; sometimes as a return type (though it is better programming practice to be more specific). The wildcard is never used as a type argument for a generic method invocation, a generic class instance creation, or a supertype.
11th Feb 2021, 7:05 AM
Soumik
Soumik - avatar
+ 3
UPPER BOUNDED WILDCARDS You can use an upper bounded wildcard to relax the restrictions on a variable. For example, say you want to write a method that works on List<Integer>, List<Double>, and List<Number>; you can achieve this by using an upper bounded wildcard. To declare an upper-bounded wildcard, use the wildcard character ('?'), followed by the extends keyword, followed by its upper bound. Note that, in this context, extends is used in a general sense to mean either "extends" (as in classes) or "implements" (as in interfaces). To write the method that works on lists of Number and the subtypes of Number, such as Integer, Double, and Float, you would specify List<? extends Number>. The term List<Number> is more restrictive than List<? extends Number> because the former matches a list of type Number only, whereas the latter matches a list of type Number or any of its subclasses. Consider the following process method: public static void process(List<? extends Foo> list) { /* ... */ }
11th Feb 2021, 7:08 AM
Soumik
Soumik - avatar
+ 3
LOWER BOUNDED WILDCARDS The Upper Bounded Wildcards section shows that an upper bounded wildcard restricts the unknown type to be a specific type or a subtype of that type and is represented using the extends keyword. In a similar way, a lower bounded wildcard restricts the unknown type to be a specific type or a super type of that type. A lower bounded wildcard is expressed using the wildcard character ('?'), following by the super keyword, followed by its lower bound: <? super A>. Note: You can specify an upper bound for a wildcard, or you can specify a lower bound, but you cannot specify both. Say you want to write a method that puts Integer objects into a list. To maximize flexibility, you would like the method to work on List<Integer>, List<Number>, and List<Object> — anything that can hold Integer values.
11th Feb 2021, 7:09 AM
Soumik
Soumik - avatar
+ 3
To write the method that works on lists of Integer and the supertypes of Integer, such as Integer, Number, and Object, you would specify List<? super Integer>. The term List<Integer> is more restrictive than List<? super Integer> because the former matches a list of type Integer only, whereas the latter matches a list of any type that is a supertype of Integer. The following code adds the numbers 1 through 10 to the end of a list: public static void addNumbers(List<? super Integer> list) { for (int i = 1; i <= 10; i++) { list.add(i); } }
11th Feb 2021, 7:10 AM
Soumik
Soumik - avatar
+ 2
And btw, D_Stark , I guess you'll have to stick to writing '? extends Number' twice 😅 because I don't think there's any other way of doing it
11th Feb 2021, 7:13 AM
Soumik
Soumik - avatar
+ 2
♨ Soumik 🎶 thanks bro that's very helpful, do you have a source were I can understand these abit better?thanks
11th Feb 2021, 8:15 AM
D_Stark
D_Stark - avatar