Upper bounded wildcard is causing compilation error [duplicate] - java

This question already has answers here:
Can't add a ModuleInfo object to ArrayList<? extends ModuleInfo>
(2 answers)
Is List<Dog> a subclass of List<Animal>? Why are Java generics not implicitly polymorphic?
(19 answers)
Closed 7 years ago.
public class Testing {
public static void main(String[] args) {
List<? extends Integer> list = new ArrayList<>();
list.add(new Integer(21));
}
}
What is the compilation error in line#4?

Related

List cannot be converted to List [duplicate]

This question already has answers here:
Is List<Dog> a subclass of List<Animal>? Why are Java generics not implicitly polymorphic?
(19 answers)
Closed 3 years ago.
Why this is not allowed?
error: incompatible types: List<TextBook> cannot be
converted to List<Book>
process(textBooks);
import java.util.*;
class Book {}
class TextBook extends Book {}
public class Sample {
public static void process(List<Book> books) {}
public static void main(String[] args) {
List<Book> books = new ArrayList<>();
process(books);
System.out.println(“OK”)
List<TextBook> textBooks = new ArrayList<>();
process(textBooks); # what is the problem in this statement?
System.out.println(“OK”);
}
}
You are trying to pass a List<TextBook> into a method whose signature expects a List<Book>. This fails at compile time, because if the Java compiler were to allow it, then your process() method might try to use the contents of the list in the wrong way. Instead, use this version of process():
public static void process(List<? extends Book> books) {}
Now you may pass in any instance of Book, or any subclass of Book.
This is not allowed because you started out with an arraylist of Textbook. However you passed it to process as a list of Book. The process function could add something which is not a textbook to the list. At this point your original list would not be an arraylist of Textbook.

Java generics - passing arguments to generic method signatures [duplicate]

This question already has answers here:
Is List<Dog> a subclass of List<Animal>? Why are Java generics not implicitly polymorphic?
(19 answers)
What is PECS (Producer Extends Consumer Super)?
(16 answers)
Closed 4 years ago.
I have the method below in my class
public void doNothing(Map<String, Collection<? extends Number>> value) {
}
and I'm calling this method with the parameter below.
private void loader() {
Map<String, ArrayList<Integer>> map = null;
doNothing(map);
}
I get the error below.
The method doNothing(Map<String,Collection<? extends Number>>) in the
type Lesson1<E> is not applicable for the arguments
(Map<String,ArrayList<Integer>>)
However if I changed the method to,
public void doNothing(Collection<? extends Number> something) {
}
and call it like below then there is no error.
private void loader() {
doNothing(new ArrayList<Integer>());
}
Can someone help me the problem with using the first method which takes in a map as an argument?
Thanks!!!

Java: Why can't List<Integer> be passed to function (List<Number>)? [duplicate]

This question already has answers here:
Is List<Dog> a subclass of List<Animal>? Why are Java generics not implicitly polymorphic?
(19 answers)
Closed 6 years ago.
Why can't we pass List<X> to a function which has List<superclass of X>? For a normal variable this is possible.
Consider the MWE below. Why can we pass Integer to a function with a Number argument, and can't we not pass List<Integer> to List<Number>?
Questions:
How can I efficiently/nicely (so without constructing a new List) work around this?
What is the rationale of this language design decision?
MWE:
import java.util.ArrayList;
import java.util.List;
public class Tester {
public static void main(String[] args) {
Integer i = new Integer(2);
// This works fine.
processNumber(i);
List<Integer> l = new ArrayList<Integer>();
// ERROR "The method processNumberList(List<Number>) in the type
// Tester is not applicable for the arguments (List<Integer>)"
processNumberList(l);
}
private static void processNumber(Number n) {
}
private static void processNumberList(List<Number> l) {
}
}
Replace your declaration with this:
private static void processNumberList(List<? extends Number> l) {...}
How can I efficiently/nicely (so without constructing a new List) work
around this?
Simply change the signature of your method as next:
private static <T extends Number> void processNumberList(List<T> l) {
}

Generic Wild Card 'Super' [duplicate]

This question already has answers here:
Why can't a Generic Type Parameter have a lower bound in Java?
(6 answers)
Bounding generics with 'super' keyword
(6 answers)
Closed 8 years ago.
I just read topics about generic wild card. "?", "extends", and "super" from SCJP 6 book. My question is related to method declaration using super and extends.
class A {
}
class B extends A {
}
class C extends B {
}
class D extends C {
}
public class Class_1 {
public static void main(String args[])
{
List<B> bs = new ArrayList<B>();
List<A> as = new ArrayList<A>();
List<C> cs = new ArrayList<C>();
List<D> ds = new ArrayList<D>();
Class_1 class_1 = new Class_1();
class_1.getName3(ds);
class_1.getName4(bs);
class_1.getName1(cs);
class_1.getName2(as);
}
public void getName3(List<? extends B> list)
{
}
public void getName4(List<? super B> list)
{
}
public <T extends B> void getName1(List<T> list)
{
}
public <T super B> void getName2(List<T> list)
{
}
}
I am getting compilation error at class_1.getName2(as);. Why ? Error is saying
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method getName2(List<T>) from the type Class_1 refers to the missing type T
at com.ksh.scjp.gNc.Class_1.main(Class_1.java:26)

Java - anonymous instance of abstract class [duplicate]

This question already has answers here:
Is List<Dog> a subclass of List<Animal>? Why are Java generics not implicitly polymorphic?
(19 answers)
Closed 8 years ago.
Type mismatch: cannot convert from BytecodeCodeProcessor<new AbstractBytecodeCodeVisitor(){}> to BytecodeCodeProcessor<AbstractBytecodeCodeVisitor>
public abstract class AbstractBytecodeCodeVisitor {
}
public class BytecodeCodeProcessor
<T extends AbstractBytecodeCodeVisitor> {
public BytecodeCodeProcessor(ClassSourceResult classSourceResult,
T visitor) {
}
}
BytecodeCodeProcessor<AbstractBytecodeCodeVisitor> processor =
new BytecodeCodeProcessor<>(classSourceResult,
new AbstractBytecodeCodeVisitor() {
});
The anonymous class instantiated via new AbstractBytecodeCodeVisitor() {} is a subclass of AbstractBytecodeCodeVisitor. This anonymous subclass is not equal to the generic type parameter specified by BytecodeCodeProcessor<AbstractBytecodeCodeVisitor>. Anonymous AbstractBytecodeCodeVisitor != AbstractBytecodeCodeVisitor, thus the compilation error. The code can be fixed in numerous ways, some of which are listed in the following link.
Generics and anonymous classes (bug or feature?)
One solution:
BytecodeCodeProcessor<AbstractBytecodeCodeVisitor> processor =
new BytecodeCodeProcessor<AbstractBytecodeCodeVisitor>(
classSourceResult, new AbstractBytecodeCodeVisitor() {}
);

Categories

Resources