Basic Programs on Generics
Generic Functionality
Generics introduced the concept of type variable. A type variable is an unqualified identifier as per the Java Language Specification, Which can be used in following ways:
- Generic class declarations
- Generic interface declarations
- Generic method declarations
- Generic constructor declarations.
class genericsDemo
{
public static void main(String[] s)
{
test<Integer> t = new test<Integer>(5);
int i = t.getI(); // No need to type cast!
System.out.println(i);
test<String> ts = new test<String>("This is demo.");
String is = ts.getI(); // No need to type cast!
System.out.println(is);
xyz o = new xyz(7);
test<xyz> txyz = new test<xyz>(o);
xyz newo = txyz.getI(); // No need to type cast!
System.out.println(newo);
}
}
class test<T> // Generic class
{
private T i;
test(T i) // Generic Constructor
{
this.i = i;
}
T getI() // Generic Method
{
return i;
}
}
class xyz
{
int x;
xyz(int x)
{
this.x = x;
}
public String toString()
{
return "Value of X is: "+x;
}
}
Above code demonstrates the use of Generic class, constructor and method. Here we are learning one more thing: It’s not a compulsion that we can only pass wrapper class objects as generic variable. A custom/User defined class’s object can also be passed as we did with: “xyz” class.
Output:
Example on Generic Method
public class GenericMethodTest
{
// generic method printArray
public static <E> void printArray( E[] inputArray )
{
// Display array elements
for ( E element : inputArray )
{
System.out.printf( "%s ", element );
}
System.out.println();
}
public static void main( String args[] )
{
// Create arrays of Integer, Double and Character
Integer[] intArray = { 1, 2, 3, 4, 5 };
Double[] doubleArray = { 1.1, 2.2, 3.3, 4.4 };
Character[] charArray = { 'H', 'E', 'L', 'L', 'O' };
System.out.println( "Array integerArray contains:" );
printArray( intArray ); // pass an Integer array
System.out.println( "\nArray doubleArray contains:" );
printArray( doubleArray ); // pass a Double array
System.out.println( "\nArray characterArray contains:" );
printArray( charArray ); // pass a Character array
}
}
Output:
Example on Generic Interface
interface MinMax<T extends Comparable<T>>
{
T min();
T max();
}
// Now, implement MinMax
class MyClass<T extends Comparable<T>> implements MinMax<T>
{
T[] vals;
MyClass(T[] o)
{
vals = o;
}
// Return the minimum value in vals.
public T min()
{
T v = vals[0];
for(int i=1; i < vals.length; i++)
if(vals[i].compareTo(v) < 0) v = vals[i];
return v;
}
// Return the maximum value in vals.
public T max()
{
T v = vals[0];
for(int i=1; i < vals.length; i++)
if(vals[i].compareTo(v) > 0) v = vals[i];
return v;
}
}
class GenIFDemo
{
public static void main(String args[])
{
Integer inums[] = {3, 6, 2, 8, 6 };
Character chs[] = {'b', 'r', 'p', 'w' };
MyClass<Integer> iob = new MyClass<Integer>(inums);
MyClass<Character> cob = new MyClass<Character>(chs);
System.out.println("Max value in inums: " + iob.max());
System.out.println("Min value in inums: " + iob.min());
System.out.println("Max value in chs: " + cob.max());
System.out.println("Min value in chs: " + cob.min());
}
}
Output:







Recent Comments