Generics - Java
Generics enable types (classes and interfaces) to be parameters when defining classes, interfaces and methods. Much like the more familiar formal parameters used in method declarations, type parameters provide a way for you to re-use the same code with different inputs. The difference is that the inputs to formal parameters are values, while the inputs to type parameters are types.
Advantage of Java Generics
There are mainly 3 advantages of generics. They are as follows:
- Type-safety : We can hold only a single type of objects in generics. It doesn’t allow to store other objects.
- Type casting is not required: There is no need to typecast the object.
- Before Generics, we need to type cast.
Using generics we cannot do the following
- Cannot Instantiate Generic Types with Primitive Types
- Cannot Create Instances of Type Parameters
- Cannot Declare Static Fields Whose Types are Type Parameters
- Cannot Use Casts or instance of With Parameterized Types
- Cannot Create Arrays of Parameterized Types
- Cannot Create, Catch, or Throw Objects of Parameterized Types
- Cannot Overload a Method Where the Formal Parameter Types of Each Overload Erase to the Same Raw Type
Programe On Generics
class abc
{
public <T> void get(T x,T y)
{
System.out.println("values of x and y"+x+" "+y);
}
}
class sample
{
public static void main(String args[])
{
abc k=new abc();
int a,b;
double x,y;
a=10;
b=40;
x=12.54;
y=2.45;
k.get(a,b);
k.get(x,y);
k.get(a,x);
k.get(y,b);
k.get("hello","vision");
}
}
Note: T extends Object class so we canot use these x and y variables in arithamatical expressions.
Find the sum of 2 nos using generics
class abc
{
public <T> void get(T x,T y)
{
Object m,n;
m=x;
n=y;
System.out.println("Sum of 2nos"+((int)m+(int)n));
}
}
class sample
{
public static void main(String args[])
{
abc k=new abc();
int a,b;
a=10;
b=40;
k.get(a,b);
}
}
Note:
To find the sum we must unbox the object to int type because the passing arguments are int type if the passing argumens are double type we must unbox them with double like so on..
Programe on Generics with arrays of wrapper classes
class abc
{
public <T> void put(T[] x)
{
for ( T k : x )
System.out.println(k);
//System.out.printf( "%s ", k );
}
}
class sample
{
public static void main(String args[])
{
Integer a[]={10,20,30,40,50};
Double x[]={3.2,5.3,23.45,54.23,34.5};
Character c[]={'a','x','k','p','s'};
String s[]={"prasaD","vision","praveen","anil"};
abc p=new abc();
p.put(a);
p.put(x);
p.put(c);
p.put(s);
}
}
Bounded type parameters
when you want to restrict the types that can be used as type arguments in a parameterized type. For example, a method that operates on numbers might only want to accept instances of Number or its subclasses
To declare a bounded type parameter, list the type parameter's name, followed by the extends keyword, followed by its upper bound, which in this example is Number. Note that, in this context, extends is used in a general sense to mean either "extends" (as in classes) or "implements" (as in interfaces).
Programe on Bounded type parametars
class abc
{
public <T extends Integer> void put(T x,T y)
{
if(x>y)
System.out.println("big"+x);
else
System.out.println("big"+y);
}
}
class sample
{
public static void main(String args[])
{
abc x=new abc();
//x.put(10,4);
x.put(10,40);
}
}
Big no out of 3nos using comparable object and generics
class sample
{
// determines the largest of three Comparable objects
public static <T extends Comparable<T>> T maximum(T x, T y, T z)
{
T max = x; // assume x is initially the largest
if ( y.compareTo( max ) > 0 ){
max = y; // y is the largest so far
}
if ( z.compareTo( max ) > 0 ){
max = z; // z is the largest now
}
return max; // returns the largest object
}
public static void main( String args[] )
{
System.out.println("Maximum value"+maximum(3,55,32));
System.out.println("Maximum value"+maximum(3.12,5.2,321.45));
System.out.println("Maximum value"+maximum("prasad","praveen","vision"));
}
}
Generic classes
A generic class declaration looks like a non-generic class declaration, except that the class name is followed by a type parameter section.
As with generic methods, the type parameter section of a generic class can have one or more type parameters separated by commas. These classes are known as parameterized classes or parameterized types because they accept one or more parameters.
Program on Generic classes
class abc<T>
{
T k;
public void getData(T x)
{
k=x;
}
public void putData()
{
System.out.println(k);
}
/* return type
public T putData()
{
return (k);
}
*/
}
class sample
{
public static void main(String args[])
{
abc<Integer> x=new abc <Integer>();
abc<String> y=new abc<String>();
abc<Double> z=new abc<Double>();
abc<String> m=new abc<>();
// we can write <> operator without datatypes.
x.getData(25);
y.getData("vision");
z.getData(35.65);
m.getData(“Prasad”);
x.putData();
y.putData();
z.putData();
m.putData();
/*System.out.println(x.putData());
System.out.println(y.putData());
System.out.println(z.putData()); */
}
}
Programe on Generic classes with multiple parameters
class abc<T,V>
{
T name;
V value;
public void getData(T x,V y)
{
name=x;
value=y;
}
public void putData()
{
System.out.println(name+" "+value);
}
}
class sample
{
public static void main(String args[])
{
abc<String,Integer> x=new abc <String,Integer>();
abc<String,Double> y=new abc<String,Double>();
abc<String,String> z=new abc<>(); // from jdk1.7 on words we can write generics instansiation like this(<>)
x.getData("CPU",25);
y.getData("HDD",34.56);
z.getData("Name","praveen");
x.putData();
y.putData();
z.putData();
}
}
Programe and polymorphisam using generics
interface prod<K,V>
{
public void getData(K x,V y);
public V putData();
}
class abc<K,V> implements prod<K,V>
{
K name;
V value;
public void getData(K x,V y)
{
name=x;
value=y;
}
public V putData()
{
return(value);
//System.out.println(name+" "+value);
}
}
class sample
{
public static void main(String args[])
{
prod<String,Integer> x=new abc <String,Integer>();
prod<String,Double> y=new abc<String,Double>();
prod<String,String> z=new abc<>();
x.getData("CPU",25);
y.getData("HDD",34.56);
z.getData("Name","praveen");
System.out.println(x.putData());
System.out.println(y.putData());
System.out.println(z.putData());
}
}
For
More Explanation
&
Online Classes
More Explanation
&
Online Classes
Contact Us:
+919885348743
+919885348743