Monday, 15 September 2014

GENERIC METHODS IMPLEMENTATION

GENERIC METHODS - 
Allows us to overload the function any data type at a particualr instance , such that we need not to write many different overloaded functions for different data types involved  .

Example - 


public class Main {
public static void main (String args[]){

//initialise string for list collection
String i [] = {"cats" , "dogs" , "mouse" , "hamsters" , "dogs"};
Integer j[] = {1 , 2 };

printMe(i);
System.out.println();
printMe(j);

}

public static <E> void printMe(E i[] ){
for(E a : i )
System.out.print(a + " ");
}
}


No comments:

Post a Comment