Generic methods and Classes
Generics are used to create such classes, interfaces, and methods with
parameters that can operate on different data types along.
Java Generic methods and generic classes enable programmers to
specify, with a single method declaration, a set of related methods, or
with a single class declaration, a set of related types, respectively.
Rules to Define Generic Methods
All generic method declarations have a type parameter section delimited
by angle brackets (< and >) that precedes the method's return type ( < E
> in the next example).
Each type parameter section contains one or more type parameters
separated by commas. A type parameter, also known as a type variable,
is an identifier that specifies a generic type name.
The type parameters can be used to declare the return type and act as
placeholders for the types of the arguments passed to the generic
method, which are known as actual type arguments.
A generic method's body is declared like that of any other method. Note
that type parameters can represent only reference types, not primitive
types (like int, double and char).
Example program without using Generic methods
Public class GenericExample
{
Public static void main(String args[])
{
display(“Satish”);
int a=2;
display(a);
float f =2.34F;
display(f);
}
Public static void display(String s)
{
System.out.println(s);
}
Public static void display(int i)
{
System.out.println(i);
}
Public static void display(float f)
{
System.out.println(f);
}
}
Example program using Generic methods
Public class GenericExample
{
Public static void main(String args[])
{
display(“Satish”);
int a=2;
display(a);
float f =2.34F;
display(f);
}
Public static <T> void display(T s)
{
System.out.println(s);
}
}
Multiple number of arguments in Generic method
Public class GenericExample
{
Public static void main(String args[])
{
display(“Satish”, “Ravi”);
int a=2,b=10;
display(a,b);
float f =2.34F, f1=3.23F;
display(f,f1);
}
Public static <T> void display(T s, T r)
{
System.out.println(s);
System.out.println(r);
}
}
Multiple number of arguments with different data types in
Generic method
Public class GenericExample
{
Public static void main(String args[])
{
display(“Satish”,123);
int a=2;
display(a, “satish”);
float f =2.34F;
display(f,a);
}
Public static <T, U> void display(T s, U r)
{
System.out.println(s);
System.out.println(r);
}
}
Without using Generic Classes
class Test
{
String s1;
String s2;
Test(String input1, String input2)
{
this.s1=input1;
this.s2=input2;
}
public void display()
{
System.out.println(this.s1);
System.out.println(this.s2);
}
}
class genericclass
{
public static void main(String arg[])
{
Test t = new Test("satish","Ravi");
t.display();
}
}
using Generic Classes
class Test<T>
{
T s1;
T s2;
Test(T input1, T input2)
{
this.s1=input1;
this.s2=input2;
}
public void display()
{
System.out.println(this.s1);
System.out.println(this.s2);
}
}
class genericclass
{
public static void main(String arg[])
{
Test <String> t = new Test <String> ("satish","Ravi");
t.display();
}
}
Test <Integer> i = new Test <Integer>(123,234);
i.display();
Two Diddernt datatypes in a class
class Test<T, U>
{
T s1;
U s2;
Test(T input1, U input2)
{
this.s1=input1;
this.s2=input2;
}
public void display()
{
System.out.println(this.s1);
System.out.println(this.s2);
}
}
class genericclass
{
public static void main(String arg[])
{
Test <String, Integer> t = new Test <String,
Integer> ("satish", 123);
t.display();
}
}