Click here to watch in Youtube :
https://www.youtube.com/watch?v=Om8i3LIX9hM&list=UUhwKlOVR041tngjerWxVccw
Click the below Image to Enlarge
Java Tutorial: Generics in java | Java Generics [The super wildcard boundary] |
public class A
{
private int value = 10;
public int getValue()
{
return value;
}
public void setValue(int value)
{
this.value = value;
}
}
B.javapublic class B extends A
{
}
C.javapublic class C extends A
{
}
GenericDemo.javaimport java.util.ArrayList;
import java.util.List;
public class GenericDemo
{
public static void main(String[] args)
{
GenericDemo genericDemo = new GenericDemo();
List<A> listA = new ArrayList<A>();
genericDemo.insertElements(listA);
System.out.println("listA = "+listA);
List<Object> listObject = new ArrayList<Object>();
genericDemo.insertElements(listObject);
System.out.println("listObject = "+listObject);
}
public void insertElements(List<? super A> list)
{
list.add(new A());
list.add(new B());
list.add(new C());
}
}
OutputlistA = [A@15db9742, B@6d06d69c, C@7852e922]
listObject = [A@4e25154f, B@70dea4e, C@5c647e05]
Click the below link to download the code:
No comments:
Post a Comment