Showing posts with label generics faciliy. Show all posts
Showing posts with label generics faciliy. Show all posts

Friday, 1 July 2011

Writing Generic interface

A class or an interface is generic if it has one or more type variable. Type variable are delimited by angle brackets and follow the class (or the interface) name:

public interface List<T> extends Collection<T> {
  ...
}

Roughly speaking, type variables act as parameters and provide the information the compiler needs to make its checks.

Many classes in the Java library, such as the entire Collections Framework, were modified to be generic. The List interface we've used in the first code snippet, for example, is now a generic class. In that snippet, box was a reference to a List<Apple> object, an instance of a class implementing the List interface with one type variable: Apple. The type variable is the parameter that the compiler uses when automatically casting the result of the get method to an Apple reference.

In fact, the new generic signature or the get method of the interface List is:

T get(int index);

The method get returns indeed an object of type T, where T is the type variable specified in the List<T> declaration.

Monday, 18 April 2011

Writing generic methods

A Generic class containing a type parameter affects the entire class, but a generic method containing one or more type parameters affects that particular method only. So it means that a non-generic class can contain a mixture of generic and non-generic methods.


Example:

public class GenericMethods
{
static <T> void printType(T anyType)
{
System.out.println(anyType.getClass().getName());
} 
 
//using generic method
public static void main(String[] args) 
{
GenericMethods.printType(String.class);
GenericMethods.printType(new String(""));
}
}

If we look at the way in which a Generic method is declared, we find that the static method printType() has a return type void and it takes a single parameter called T. Here T stands for any parametric type which can be substituted with any of the Java types by the Clients. Since we have introduced a parameter T, it should be defined. But where? It should be defined in the method definition itself just before the return type of the method ().

The moral is whenever we have different type parameters in a method, it should be defined in the method definition. For example, consider the following method that has two type parameters A and B and they are defined before the return type of the method separated by commas.


<A, B> void genericMethod2Parameters(A aType, B bType)
{
// Something here.
}


Writing generic class in java

Consider the following class:

package com.vaani.generics.classes;

public class MyGenericClass<T>
{
private T anyObject;

public T getObject()
{
return anyObject;
}

public void setObject(T anyObject)
{
this.anyObject = anyObject;
}

public String toString()
{
return anyObject.toString();
}
}

Note the following syntax :
public class MyGenericClass<T>
The above statement essentially says that we wish to make the MyGenericClass class as a Generic Class. Technically, it is now a parametric class and T is called a type parameter.

T serves as a place-holder for holding any type of Object. Note the usage of the type parameter within the class declaration. Now, the clients can use the above class by substituting any kind of object for the place-holder T.

public class MyGenericClassRunner
{
public static void main(String[] args) throws Exception
{
MyGenericClass<String> stringHolder =
new MyGenericClass<String>();
stringHolder.setObject(new String("String"));
System.out.println(stringHolder.toString());

MyGenericClass<URL> urlHolder = new MyGenericClass<URL>();
urlHolder.setObject(new URL("http://www.k2java.blogspot.com"));
System.out.println(urlHolder.toString());
}
}

Though parameter is placeholder, its much different from templates in c++.

Note how the clients or users of this class instantiates an instance for the MyGenericClass class. 
MyGenericClass<String> stringHolder = 
new MyGenericClass<String>();

This is called type substitution. For the type parameter O, the type String is substituted.

Generic classes having two or more parametric types

You can simply write:
public class My2ParameterClass<A, B>

So instead of 1 parameter have 2 parameters and initialize etc. like original.