Having Issue with Bounded Wildcards in Generic
- by Sanjiv
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 ?