Generic Classes

as functions classes can also be generic which accepts data types of any type variables. The syntax to define a generic class as :

class Gen<T> {
	T ob; // declare an object of type T

	// Pass the constructor a reference to
	// an object of type T.

	Gen(T o) {
		ob = o;
	}

	// Return ob.
	T getob() {
		return ob;
	}
}

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

		Gen<Integer> iOb; // creating generic class object 1
		iOb = new Gen<Integer>(88); // assigning integer type
		int v = iOb.getob();
		System.out.println("value: " + v);
		System.out.println();

		// creating generic class object 2 with string type
		Gen<String> strOb = new Gen<String>("Generics Test");
		String str = strOb.getob();
		System.out.println("value: " + str);
	}
}