Java Generics Class with Two Type Parameters

Introduction

We can declare more than one type parameter in a generic type.

//A simple generic class with two type parameters: T and V. 
class TwoGen<T, V> { 
  private T ob1; 
  private V ob2; 
   //from  www .j av  a 2s . c o m
  // Pass the constructor a reference to  
  // an object of type T. 
  public TwoGen(T o1, V o2) { 
    ob1 = o1; 
    ob2 = o2; 
  } 
 
  // Show types of T and V. 
  public void showTypes() { 
    System.out.println("Type of T is " + 
                       ob1.getClass().getName()); 
 
    System.out.println("Type of V is " + 
                       ob2.getClass().getName()); 
  } 
 
  public T getob1() { 
    return ob1; 
  } 
 
  public V getob2() { 
    return ob2; 
  } 
} 
 
// Demonstrate TwoGen. 
public class Main { 
  public static void main(String args[]) { 
 
    TwoGen<Integer, String> tgObj = 
      new TwoGen<Integer, String>(88, "Generics"); 
 
    // Show the types. 
    tgObj.showTypes(); 
 
    // Obtain and show values. 
    int v = tgObj.getob1(); 
    System.out.println("value: " + v); 
 
    String str = tgObj.getob2(); 
    System.out.println("value: " + str); 
  } 
}



PreviousNext

Related