CodesJava

Easy learning with example program codes

Generics class example with two parameters


Generics class with two parameters in java and generics interface with two parameters in java:

Generic classes/interfaces have the class name followed by a type parameter section. Type parameter section of can have one or more type parameters separated by commas. These classes/interfaces are known as parameterized classes/interfaces.

Generics class example with two parameters:

GenericsTest.java

/**
 * This class is used to show the use generics 
 * class with two parameters.
 * @author codesjava
 */
class Test<T, U> {
  private T itemT;
  private U itemU;
 
  public Test(T itemT, U itemU){
	  this.itemT = itemT;
	  this.itemU = itemU;
  }
 
  public T getItemT() {
	return itemT;
  }	
 
  public void setItemT(T itemT) {
	  this.itemT = itemT;
  }
 
  public U getItemU() {
	  return itemU;
  }
 
  public void setItemU(U itemU) {
	  this.itemU = itemU;
  }
 
  public void showItemDetails(){
	  System.out.println("Value of the itemT: " 
			  + itemT);
	  System.out.println("Type of the itemT: " 
			  + itemT.getClass().getName());
	  System.out.println("Value of the itemU: " 
			  + itemU);
	  System.out.println("Type of the itemU: " 
			  + itemU.getClass().getName());
  }
}
 
public class GenericsTest {	
	public static void main(String args[]){
		//String type test
		Test<String, Integer> test = 
		  new Test<String, Integer>("Test String.", 100);
		test.showItemDetails();
	}
}

Output:

Value of the itemT: Test String.
Type of the itemT: java.lang.String
Value of the itemU: 100
Type of the itemU: java.lang.Integer

Download this example.
 
Next Topic: Generics method example.
Previous Topic: Generics class example.

Related Topics:

How to build java project using ant in eclipse?
JAXB marshalling – convert java object to xml example.
How to create pdf file in java using iText jar?
Generics class example.
OGNL in struts 2.
Hibernate One-to-One Mapping using xml.
Send inline image in email using JavaMail API.
Quartz 2 JobListener example.

 



Industrial Training

We offers Placement Oriented Training on Java, Spring, JSF, Hibernate, PHP, AngularJS, Angular 4, PLSQL, Oracle BI Publisher etc. We also provides Online training, please mail us at hr@codesjava.com.

Development

We also provides the Development services for Website Development , Java Development, PHP Development, Android App Development etc. You can contact us on hr@codesjava.com.

Copyright © 2019 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki