Wednesday 14 May 2014

Generics in JAVA



What is Generics in Java ??


Generics are a facility of generic programming that were added to theJava programming language in 2004 within J2SE 5.0. They allow “a type or method to operate on objects of various types while providing compile-time type safety.” This feature specifies the type of objects stored in a Java Collection.

Generics add a way to specify concrete types to general purpose classes and methods that operated on Object .Generics also provide compile-time type safety that allows programmers to catch invalid types at compile time.

Using Java Generic concept, we might write a generic method for sorting an array of objects, then invoke the generic method with Integer arrays, Double arrays, String arrays and so on, to sort the array elements.

Following are the rules to define Generic Methods:

  • All generic method declarations have a type parameter section delimited by angle brackets (< and >) that precedes the method’s return type ( < E > in the next example).
  • Each type parameter section contains one or more type parameters separated by commas. A type parameter, also known as a type variable, is an identifier that specifies a generic type name.
  • The type parameters can be used to declare the return type and act as placeholders for the types of the arguments passed to the generic method, which are known as actual type arguments.
  • A generic method’s body is declared like that of any other method. Note that type parameters can represent only reference types, not primitive types (like int, double and char).

Simple generics class example


public class SimpleGenericsApp {

public static void main(String a[]) {
SimpleGenerics genericString = new SimpleGenerics(“MY-GENERICS”);
genericString.printType();

SimpleGenerics genericInteger = new SimpleGenerics(10);
genericInteger.printType();
}

}

class SimpleGenerics<T> {

private T myobj = null;
private List<T> list = new ArrayList<T>();

public SimpleGenerics(T t) {
myobj = t;
}

public T getObject() {
return myobj;
}

public void add(T t) {
this.list.add(t);
}

public List getList(T t) {
return this.list;
}

public void printType() {
System.out.println(“Type: ” + myobj.getClass().getName());
}
}


Bounded Type:


There may be times when you’ll want to restrict the kinds of types that are allowed to be passed to a type parameter. For example, a method that operates on numbers might only want to accept instances of Number or its subclasses. This is what bounded type parameters are for.

To declare a bounded type parameter, list the type parameter’s name, followed by the extends keyword, followed by its upper bound.

class BoundedGenerics<T extends A> {
private T objRef;

public BoundedGenerics(T obj) {
this.objRef = obj;
}

public void doRunTest() {
this.objRef.printClass();
}
}

class A {
public void printClass() {
System.out.println(“I am in super class A”);
}
}

class B extends A {
public void printClass() {
System.out.println(“I am in sub class B”);
}
}

class C extends A {
public void printClass() {
System.out.println(“I am in sub class C”);
}
}

public class BoundedExample {
public static void main(String a[]) {

BoundedGenerics<C> bec = new BoundedGenerics<C>(new C());
bec.doRunTest();

BoundedGenerics<B> beb = new BoundedGenerics<B>(new B());
beb.doRunTest();

BoundedGenerics<A> bea = new BoundedGenerics<A>(new A());
bea.doRunTest();

}
}