wellnwill Marathahalli Bangalore

Java Generics

Generic provides type safety and to resolve type casting problem. Collection can store any type of object. Due to this it is not type safe and type casting problem occurs at runtime i.e. ClassCastException.

Java array is type safe and contains similar type of elements so if we will try to insert heterogeneous element, we will get compile time error. Hence type compatibility will be checked at compile time.

class TestG
{
public static void main(String args[])
{
int a[]=new int[5];
a[0]=1;
a[1]=2;
a[2]="Dheeraj";//Compile time error
}
}
//O/P-CTE
public class Employee{  
  int id;  
  String name;  
  int salary;  
 public Employee(int id,String name,int salary){  
   this.id=id;  
   this.name=name;  
   this.salary=salary;  
  }  
}


public class Programmer{  
  int id;  
  String name;  
  int salary;  
public Programmer(int id,String name,int salary){  
   this.id=id;  
   this.name=name;  
   this.salary=salary;  
  }  
}


import java.util.*;  
public class Test236{  
 public static void main(String args[]){  
  Employee e1=new Employee(501,"Raj",4000);  
  Employee e2=new Employee(502,"Dev",5000);  
  ArrayList al=new ArrayList(); 
  al.add(e1);  
  al.add(e2);  
    
  Iterator itr=al.iterator();   
  while(itr.hasNext()){  
    Employee e=(Employee)itr.next();  
    System.out.println(e.id+" "+e.name+" "+e.salary);  
  }  
 }  
}  



import java.util.*;  
public class Test237{  
 public static void main(String args[]){  
  Employee e1=new Employee(501,"Raj",4000);  
  Employee e2=new Employee(502,"Dav",5000); 
  Programmer p1=new Programmer(503,"Jon",4500);  
  ArrayList al=new ArrayList(); 
  al.add(e1);  
  al.add(e2);
  al.add(p1);  
  Iterator itr=al.iterator();   
  while(itr.hasNext()){  
    Employee e=(Employee)itr.next();  
    System.out.println(e.id+" "+e.name+" "+e.salary);  
  }  
 }  
//o/p-Runtime Exception 
//i.e. ClassCastException

import java.util.*;  
public class Test238{  
 public static void main(String args[]){  
  Employee e1=new Employee(501,"Raj",4000);  
  Employee e2=new Employee(502,"Dav",5000); 
  Programmer p1=new Programmer(503,"Joh",4500);  
  ArrayList al=new ArrayList(); 
  al.add(e1);  
  al.add(e2);
  al.add(p1);  
 Iterator itr=al.iterator();   
  while(itr.hasNext()){ 
    Object obj=itr.next();
	if(obj instanceof Employee)
	{
             Employee e=(Employee)obj;
	System.out.println(e.id+" "+e.name+" "+e.salary); 
    } 
	}  
 }  
 }
// O/P-501 Raj 4000
     //502 David 5000

import java.util.*;  
public class Test239{  
 public static void main(String args[]){  
  Employee e1=new Employee(501,"Raj",4000);  
  Employee e2=new Employee(502,"Dav",5000); 
  Programmer p1=new Programmer(503,"Jon",4500);  
  ArrayList al=new ArrayList(); 
  al.add(e1);  
  al.add(e2);
  //al.add(p1);  cannot add object other then Employee
   Iterator itr=al.iterator();   
   while(itr.hasNext()){  
   Employee e=(Employee)itr.next();  
   System.out.println(e.id+" "+e.name+" "+e.salary);  
  }  
 }  
}   








We can apply polymorphic nature only with base type not for parameter type.

Footer with Map

Top