I have two objects as follows
public class MyObject1 implements Serializable
{
private Long id;
... other properties and getters and setters
}
public class MyObject2 extends MyObject1
{
private String name;
...other properties and getters and setters
}
MyObject1 obj1 = new MyObject1();
MyObject2 obj2 = new MyObject2();
How do i add these two instances in a HashMap using generics?
Update
I want to be able to add MyObject1 and MyObject2 in the same map.
Like
Map<Long, ? extends MyObject1> map;
so that i can do this
map.put(obj1);
map.put(obj2);
Hope it is clearer now.
Let’s assume the id is the key. It would be something like this: