I tried to understand the concept and rationale of the lower restricted template in Java Generics. I can understand the reasoning behind the upper bounded wildcard for read-only and where and how to use it. I am still not able to understand the bottom restricted template. I have a set of classes that follow below the hierarchy. ( Automobile- base class.)
Automobile
- Bus
- Minibus
- Doubledecker
- Electricbus
- Car
- Sedan
- Hatchback
- Coupe
- Truck
- Minivan
- Pickuptruck
- Suv
- Fullsuv
- Midsuv
Now I'm trying to create a (flexible) list and add objects of different types to it.
import java.util.ArrayList;
import java.util.List;
public class Garage {
public static void main(String[] args)
{
List<? super Suv> list = null;
Suv s = new Suv();
Truck t = new Truck();
Automobile a = new Automobile();
list = new ArrayList<Suv>();
list.add(s);
list = new ArrayList<Truck>();
list.add(t);
list = new ArrayList<Automobile>();
list.add(a);
}
}
Suv List<? super Suv>. , Truck Automobile . List<? super Suv> , Suv ? Truck Automobile . ?