Menu
Topics Index
...
`

Generics Lower bounded Wildcard - Java Example Program


Generics Lower bounded Wildcard
import java.util.ArrayList;
import java.util.List;

class GenericsWithLowerBoundedWildCardsDemo
{
    public static void main(String[] args)
    {
        List<Object> list = new ArrayList<Object>(); // LINE A
        
        addIntegers(list); // LINE B
        
        for(Object value : list)
        {
            System.out.print(value +" ");
        }
    }
    public static void addIntegers(List<? super Integer> list)
    {
        for(int i = 1; i < 5; i++)
        {
            list.add(i);
        }
    }
}
OUTPUT

1 2 3 4

DESCRIPTION

At LINE A we create an instance for object list and at LINE B we invoke addIntegers method. It adds integers from 1 to 5. It supports to add to the list.

THINGS TO TRY
  • Create one more method to add Doubles and invoke the method instead of addIntegers and print the values. Put lower bound class as Double in the new method.

© meritcampus 2019

All Rights Reserved.

Open In App