Monday, April 6, 2009

generic-class-generic-method

Generic classes:
We are allowed to define type parameter for the classes.such type of parametrized classes are called generic classes.

//Type parameter T
class Generictest<T,X>
{
T ob; //Generic variable

Generictest(T ob) //Generic constructor
{
this.ob=ob;
}
void show()
{
System.out.println(the type is:ob.getClass().getName());
}
T getob() //Generic method
{
return ob;
}
}
class GenericDemo
{
public static void main(Strin arg[])
{
Generictest<Integer> g=new Generictest<Integer>(10);
g.show(); //java.lang.Integer
System.out.println(g.getob()); // 10

Generictest<String> g=new Generictest<String>(“Laxman”);
g.show(); //java.lang.String
System.out.println(g.getob()); // Laxman
}
}

Bound Types:
We can bound the type parameter I the generic classes by using extends keyword.

Example:
Gen<T>
{
}
We are allowed to create an instance of class by passing any parameter type.
Gen<T extends Number>
{
}

  • we can create a gen object by passing the type parameter which must be child class of number violation leads to error.
i.e Gen<String> g=new Gen<String>();
CTE saying type parameter java.lang.String is not with in its bound.
Extends means both extends as well as implements.

class Gen<T extends X>
{
}
If X is a class any child class of X (including X) is allowed as T.
If X is any interface Any implemented class of X is allowed as T.

Generic Methods:
class
{
public static void main(String arg[])
{
ArrayList<String> st=new ArrayList<String>();
st.add(“aaa”);
st.add(“bbb”);
st.add(“ccc”);
show(s1);

ArrayList<Integer> st=new ArrayList<Integer>();
st.add(10);
st.add(20);
st.add(30);
show(s2);
public static void main(String arg[])
{
System.out.println(s);
}
}
}
wild card(?)character in generic conclusions:
1.m1(ArrayList<String> l)
This can be applied only for ArrayList of String type only.
2.m1(ArrayList<?> l)
This can be applied for ArrayList of any type ? means unknown type.which is known as wild card character .
3.m1(ArrayList<? Extends X> l)
If X is class this method can be applied for ArrayList of any class ,which is the child class of X is allowed.
If X is interface then any class which implements X is allowed .
4.m1(ArrayList <? Super X> l)
any class which is super class of X is allowed as the type parameter.

Conclusions:
class Test
{
public static void main(Strin arg[])
{
ArrayList<String> l=new ArrayList<String>();
l.add(“a”);
l.add(“b”);
l.add(“c”);
show(l);
//If type is string
show(ArrayList<String> l)
{
l.add(“e”);
l.add(10); // invalid (error: we are only allowed to add String objects)
System.out.println(l); // [a,b,c,e]
}
//If type is wild char (?)
show(ArrayList<?> l)
{
l.add(“e”); // error
l.add(“null”); // valid
System.out.println(l);
}
}

  • Here we doesn’t know what kind of parameter is coming for this method.Because this method is applicable for Arraylist of any type of parameter.
  • If we use wild card character ‘?’ in the method declaration ,then we are allowed to perform only READ operation with in that method.
  • we are not allowed to add any element because what kind of arraylist is coming as argument we can’t give any assurance.
  • But we can add null because null is a valid value for any object type.
  • We are allowed to use wild card character ? in the declaration part but we are not allowed to use in the construction part (Right hand side)
Example:
ArrayList<?> l=new ArrayList<String>(); //valid
ArrayList<?> l=new ArrayList<Integer>(); //valid
ArrayList<? Extends Number> l=new ArrayList<Integer>(); //valid
ArrayList<? Extendes Number> l=new ArrayList<Integer>(); // not valid
CTE:incompatable types
Found:ArrayList<string>
Reqire:ArrayList<Number>

ArrayList<String> l=new ArrayList<?>(); //not valid
CTE: unexcepetedtype found:?
Req: class or interface with out bounds.
  • On the right hand side we are not allowed to impose any boundaries by using extends or super key words.
Communication with legacy code:
To provide support for legacy code Sun people compromised some rules of generics.
class Test
{
public static void main(String arg[])
{
ArrayList <String> l=new ArrayList<String> ();
l.add(“a”);
l.add(“b”);
l.add(“c”);
show(l);
}
public static void show(ArrayList l)
{
l.add(“e”);
l.add(new Integer(10));
System.out.println(l); // [ a,b,c ,d,e,10]
}
  • Here we won’t get any errors but we get some warning.
  • To sport 1.4 version methods Sun people some how compromised in this generics area in 1.5 version.
ArrayList l=new ArrayLiat<String>(); // valid

0 comments:

Post a Comment