Friday, 26 May 2017

Java Generics ~ GNIITHELP

A class or interface that operates on parameterized type is called Generic. Generics was first introduced in Java5. Now it is one of the most profound feature of java programming language. It provides facility to write algorithm independent of any specific type of data. Generics also provide type safety.
Using Generics, it becomes possible to create a single class or method that automatically works with all types of data(Integer, String, Float etc). It expanded the ability to reuse code safely and easily.

Example of Generic class

class Gen <T>
{
 T ob;     //an object of type T is declared
 Gen(T o)  //constructor
 {
  ob = o;
 }
 public T getOb()
 {
  return ob;
 }
}

class Test
{
 public static void main (String[] args)
 {
  Gen < Integer> iob = new Gen(100);     //instance of Integer type Gen Class. 
  int  x = iob.getOb();
  System.out.print in(x);
  Gen < String> sob = new Gen("Hello");  //instance of String type Gen Class.
  String str = sob.getOb();
 }
}
Output :

100
Hello

Generics Work Only with Objects

You cannot use primitive datatype such as int or char etc with Generics type. It should always be an object.
Gen< int> iOb = new Gen< int>(07);    //Error, can't use primitive type

Generics Types of different Type Arguments are never same

Reference of one generic type is never compatible with other generic type unless their type argument is same. In the example above we created two objects of class Gen, one of type Integer, and other of type String, hence,
iob = sob;    //Absolutely Wrong

Generic Methods

You can also create generic methods that can be called with different types of arguments based on the type of arguments passed to generic method, the compiler handles each method.

Example of Generic method

class GenTest
{
 static < V, T> void display (V v, T t)
 {
  System.out.println(v.getClass().getName()+" = " +v);
  System.out.println(t.getClass().getName()+" = " +t);
 }
 public static void main(String[] args)
 {
  display(88,"This is string");
 }
}
Output :
java lang.Integer = 88
Java lang.String = this is string 

Generic Constructors

It is possible to create a generic constructor even if the class is not generic.

Example of Generic Constructor

class Gen
{
 private double val;
 < T extends Number> Gen(T ob)
 {
  val=ob.doubleValue();
 }
 void show()
 {
  System.out.println(val);
 }
}

class Test
{
 public static void main(String[] args)
 {
  Gen g = new Gen(100); 
  Gen g1 = new Gen(121.5f);
  g.show();
  g1.show(); 
 }
}
Output :
100.0
121.5 

Generic Interface

Like classes and methods, you can also create generic interfaces.
interface MyInterface< T >
{ .. }

Generic Bounded type Parameter

You can also set restriction on the type that will be allowed to pass to a type-parameter. This is done with the help of extends keyword when specifying the type parameter.
 < T extends Number >
Here we have taken Number class, it can be any wrapper class name. This specifies that T can be only be replaced by Number class data itself or any of its subclass.

Generic Method with bounded type Parameters.

class Gen
{
 static < T, V extends number> void display(T t, V v)  
 {
  System.out.println(v.getClass().getName()+" = " +v);
  System.out.println(t.getClass().getName()+" = " +t);
 }
 public static void main(String[] args)
 {
 // display(88,"This is string");
  display ("this is string",99); 
 }
}
Output :
  java.lang.String = This is string 
  java.lang.Double = 99.O

  • Type V is bounded to Number type and its subclass only.
  • If display(88,"This is string") is uncommented, it will give an error of type incompatibility, as String is not a subclass of Number class.

No comments:

Post a Comment