BEHAVE EXACTLY THE SAME AS TEMPLATES IN C++
Does not work on primitive types unlike C++
A way for the user to implement one class that can handle all types of data
A way for the user to implement a method
Example of A Generic Class
class Main {
public static void main (String [] args ) {
// initialize generic class
// with Integer data
GenericsClass <Integer > intObj = new GenericsClass <>(5 );
System .out .println ("Generic Class returns: " + intObj .returnTheData ());
// initialize generic class
// with String data
GenericsClass <String > stringObj = new GenericsClass <>("Java Programming" );
System .out .println ("Generic Class returns: " + stringObj .returnTheData ());
}
}
class GenericsClass <T > {
// variable of T type
private T data ;
//Constructor of A Generics Class
public GenericsClass (T data ) {
this .data = data ;
}
// method that return T type variable
public T returnTheData () {
return this .data ;
}
}
Example Of A Generic Method
class Main {
public static void main (String [] args ) {
// initialize the class with Integer data
CallingClass call = new CallingClass ();
// generics method working with String
call .<String >genericsMethod ("429 and 727225 are nelan's lucky numbers" );
// generics method working with integer
call .<Integer >genericsMethod (25 );
}
}
class CallingClass {
// creae a generics method
public <T > void genericsMethod (T data ) {
System .out .println ("Generics Method:" );
System .out .println ("Data Passed In: " + data );
}
}