class Test<T>
{
void method1(T t){ }
<T> method2(){ }
Test <T>() { }
}
ArrayList<String> list = new ArrayList<String>();
List.add("Java");
String str = list.get(0);
import java.util.*;
public class GenDemo
{
public static void main(String args[])
{
ArrayList<String> list = new ArrayList<String>();
list.add("DigVijay");
list.add("Surendra");
list.add("Pravin");
//list.add(101); compile time error
Iterator<String> itr = list.iterator();
while(itr.hasNext())
{
String str = itr.next();
System.out.println("Name: "+str.toUpperCase());
}
}
}
class class_name<type_param_list>
{
class_name<type_arg_list> var_name = new class_name<type_arg_list>();
}
public class Test<T>
{
private T t;
public void insert(T t)
{
this.t = t;
}
public T get()
{
return t;
}
public static void main(String[] args)
{
Test<Integer> in = new Test<Integer>();
Test<String> str = new Test<String>();
Test<Float> fl = new Test<Float>();
in.insert(new Integer(101));
str.insert(new String("Java"));
fl.insert(new Float(20.30));
System.out.printf("Integer Value: %d", in.get());
System.out.printf("\nString Value: %s", str.get());
System.out.printf("\nFloat value: %f", fl.get());
}
}
import java.util.*;
public interface Comparable<T>
{
public int compareTo(T o)
}
public class GenMethDemo
{
static <T, V extends T> boolean genMeth(T x, V[] y)
{
for(int i = 0; i < y.length; i++ )
if(x.equals(y[i]))
{
return true;
}
return false;
}
public static void main(String args[])
{
Integer arr[] = {1, 2, 3, 4, 5, 6};
if(genMeth(3, arr))
{
System.out.println("3 is present in array");
}
if(!genMeth(9, arr))
{
System.out.println("9 is not present in array");
}
String strs[] = { "Java", "C++", "Oracle", "Python"};
if(genMeth("Java", strs))
{
System.out.println("Java is present in String array");
}
if(!genMeth("PHP", strs))
{
System.out.println("PHP is not present in String array");
}
}
}
import java.util.*;
abstract class Vehicle
{
abstract void speed();
}
class Bike extends Vehicle
{
void speed()
{
System.out.println("Speed of Bike is: 50 Km/h");
}
}
class Car extends Vehicle
{
void speed()
{
System.out.println("Speed of Car is: 70 Km/h");
}
}
class Bus extends Vehicle
{
void speed()
{
System.out.println("Speed of Bus is: 60 Km/h");
}
}
public class GenericDemo
{
public static void findSpeed(List<? extends Vehicle> lists)
{
for(Vehicle v : lists)
{
v.speed();
}
}
public static void main(String args[])
{
List<Bike> list1 = new ArrayList<Bike>();
list1.add(new Bike());
List<Car> list2 = new ArrayList<Car>();
list2.add(new Car());
List<Bus> list3 = new ArrayList<Bus>();
list3.add(new Bus());
findSpeed(list1);
findSpeed(list2);
findSpeed(list3);
}
}