wellnwill Marathahalli Bangalore

Generic Method

Generic method can accept any type of argument.


Case 1: m1(ArrayList l)

We can call this method by passing ArrayList of only number type and within the method we can add only Number type of object.

m1(ArrayList l)
{
add(10);   //valid
add(“String”);//invalid
}
import java.util.*;
class TestG5
{
void m1(ArrayList l)
{
l.add(10);//valid
// l.add("String");//invalid
System.out.println(l);
}
public static void main(String args[])
{
ArrayList l1=new ArrayList();
TestG5 t1=new TestG5();
t1.m1(l1);
}
}

Case 2:m1(ArrayList < ? > l)

We can call this method by passing arraylist of any type. But within the method we cannot add anything to the list except null because we do not know the type exactly.

Null is allowed because it is valid value for any type.

import java.util.*;
class TestG7
{
void m2(ArrayList < ? > l)
{
//l.add(10);//invalid
// l.add("String");//invalid
System.out.println(l);
}

public static void main(String args[])
{
ArrayList l1=new ArrayList();
TestG7 t1=new TestG7();
t1.m2(l1);
ArrayList sl1=new ArrayList();
t1.m2(sl1);
}
}

Case 3: m1(ArrayList< ? extends Z > al)

Z can be represented as class or interface. Suppose Z is a class then we will call this method by using ArrayList of Z type or its sub classes.

Suppose Z is an interface then we will call this method by using ArrayList of Z type or its implementation classes.

By using this method we cannot add any type of elements to the list except null because JVM does not know the type exactly.

import java.util.*;
class TestG8
{
void m2(ArrayList< ?  extends Number > al)
{
 //al.add(10);//CTE
 //al.add("String");//CTE
  al.add(null);
System.out.println(al);
}
public static void main(String args[])
{
ArrayList l1=new ArrayList();
TestG8 t1=new TestG8();
t1.m2(l1);
ArrayList sl1=new ArrayList();
t1.m2(sl1);
/*ArrayList stl1=new ArrayList();
t1.m2(stl1);*/ CTE
}
}
//O/P-null,null

Case 4: m2(AL < ? super Z > al)

Z can be represented as class or interface. if Z is a class then we will call this method by using ArrayList of Z type or its parent classes. if Z is a interface then we will call this method by using ArrayList of Z type or parent class of implementation class of Z. By using this method we can add Z type of elements to the list and null.

import java.util.*;
class TestG9
{
void m2(ArrayList< ?  super Number> al)
{
 al.add(10);
 //al.add("String");//CTE
  al.add(null);
System.out.println(al);
}
public static void main(String args[])
{
ArrayList l1=new ArrayList();
TestG9 t1=new TestG9();
t1.m2(l1);
ArrayList < Object > sl1=new ArrayList < Object >();
t1.m2(sl1);
/*ArrayList < String > stl1=new ArrayList < String >();
t1.m2(stl1); */ CTE
}
}
import java.util.*;
class TestG11
{
public static void main(String args[])
{
ArrayList < Number > l1=new ArrayList < Number >();
ArrayList < String > l2=new ArrayList < String >();
ArrayList < Object > sl1=new ArrayList < Object >();
ArrayList < ? > l3=new ArrayList < Number > ();
ArrayList < ?  extends String> l4=new ArrayList < String >();
//ArrayList < ?  extends String> l5=new ArrayList < Number >();
ArrayList < ?  super String> l6=new ArrayList < String > ();
ArrayList < ?  super String> l7=new ArrayList < Object >();
//ArrayList < ? > stl1=new ArrayList< ? >();
//ArrayList < ? > stl2=new ArrayList< ?  extends String >();
//ArrayList < ? > stl3=new ArrayList< ?  super String >();
}
}
Footer with Map

Top