Having Issue with Bounded Wildcards in Generic
Posted
by
Sanjiv
on Stack Overflow
See other posts from Stack Overflow
or by Sanjiv
Published on 2013-06-30T05:55:20Z
Indexed on
2013/06/30
10:21 UTC
Read the original article
Hit count: 256
I am new to Java Generics, and I'm currently experimenting with Generic Coding....final goal is to convert old Non-Generic legacy code to generic one...
I have defined two Classes with IS-A i.e. one is sub-class of other.
public class Parent {
private String name;
public Parent(String name) {
super();
this.name = name;
}
}
public class Child extends Parent{
private String address;
public Child(String name, String address) {
super(name);
this.address = address;
}
}
Now, I am trying to create a list with bounded Wildcard. and getting Compiler Error.
List<? extends Parent> myList = new ArrayList<Child>();
myList.add(new Parent("name")); // compiler-error
myList.add(new Child("name", "address")); // compiler-error
myList.add(new Child("name", "address")); // compiler-error
Bit confused. please help me on whats wrong with this ?
© Stack Overflow or respective owner