Suggested Pages

Thursday, November 14, 2013

Java Generics Example

Suppose we have the following class:

 public interface Pc {}

  class Apple implements Pc {}

  class Win implements Pc {}

  class Iphone extends Apple {}

  class Ipod extends Apple {}



Generics Statements


The following statements are correct:

    List<Apple> apples=new LinkedList<Apple>();
   List<Iphone> iphones=new LinkedList<Iphone>();
   List<Ipod> ipods=new LinkedList<Ipod>();
Instead the following statements are not correct:

   List<Apple> apples=new LinkedList<Iphone>(); // Compiler error

   List<Apple> apples=new LinkedList<Ipod>(); // Compiler error
  


Wildcard

Wildcard allows methods, classes, constructors to accept any type as argument. For example public void doThis (List<?> list) means you can pass to this method any type.

Generics Lower Bound


It's possible to specify an lower bound to wildcard using this signature: ? super type.
In the following snippet, you can see a method doThis that has a parameter List<? super Apple> lista which means that you can pass to the method any super type of Apple.

  ...
  public static void doThis(List< ? super Apple> list) {

        list.add(new Apple());   
        list.add(new Iphone());  
        list.add(new Ipod());   
  }

  public static void main(String[] args){
       
        List<Apple> apples=new LinkedList<Apple>();
        List<Pc>pcs=new LinkedList<Pc>();
        doThis(apples);
        doThis(pcs);
  }



Generics Upper Bound


It's possible to specify an upper bound to wildcard using this signature: ? extends type.
In the following snippet, you can see a method doThis that has a parameter List<? extends Apple> lista which means that you can pass to the method any super type of Apple.

Example-3


  ...

   public static void doThis(List< ? extends Apple> list) {
     list.add(null);
     List<Apple>listAux=(List<Apple>)list; 
     listAux.add(new Iphone());  
     listAux.add(new Ipod());  
   }

   public static void main(String[] args){
     List<Iphone> iphones=new LinkedList<Iphone>();
     List<Ipod> ipods=new LinkedList<Ipod>();
     List<Apple> apples=new LinkedList<Apple>();
     doThis(iphones);
     doThis(ipods);
     doThis(apples);
  }





Generics Statements with Upper Bound and Lower Bound


The following snippet compiles successfully.

List<? extends Apple> apples= new LinkedList<Apple>();
List<? super Apple> apples= new LinkedList<Apple>();



Generics Class


Esempio-1

You cannot make a static reference to the non-static type T. T and E are instance and not static properties
class GenericsList<T,E>{
   static T t; // Compiler Error 
}


class GenericsList<T,E>{
   public static void doThis(T t){} // Compiler Error

}

Esempio-2

Compile successfully.

class GenericsList<T,E>{
 T t;
 E e;
}

Esempio-3

E can be any subclass of Number.

class GenericsList<T,E extends Number> {
  T t;
  E e;
}

Esempio-4

You cannot use super keyword at class level
class GenericsList<E super Integer> {        // Compiler Error
  E e;
}

Esempio-5

You cannot use ? keyword at class level

 class GenericsList<? extends Number> {      // Compiler Error
 } 



Generics Method


Esempio-1


Compile successfully.

class GenericsList<T extends Number> {
    T t;
    public <V extends Number> void doThis(V v) {
    }
}

Esempio-2

You cannot use super keyword in generics methods.

class GenericsList<T extends Number> {               
  T t;
  public <V super Integer> void doThis(V v){   // Compiler error
  }
}

Esempio-3

Compile successfully.

class GenericsList<T extends Number> {               

   public void doThis(List<T> lista){   
    for(Number n:lista){
     System.out.println(n);
   }
}
}

Esempio-4

Compile successfully.

class GenericsList<T extends Number> {               

   public void doThis(List<T> lista){   
     for(Number n:lista){
       System.out.println(n);
   }
}

No comments :

Post a Comment

Suggested Pages