Thursday, 8 June 2017

Java Tutorial: Generics in java | Java Generics [The super wildcard boundary] ~ foundjava


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] 
A.java
public class A
{
    private int value = 10;

    public int getValue()
    {
        return value;
    }

    public void setValue(int value)
    {
        this.value = value;
    }

}
B.java
public class B extends A
{

}
C.java
public class C extends A
{

}
GenericDemo.java
import 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());
    }

}
Output
listA = [A@15db9742, B@6d06d69c, C@7852e922]
listObject =  [A@4e25154f, B@70dea4e, C@5c647e05]
Click the below link to download the code:

CLICK HERE

No comments:

Post a Comment