wellnwill Marathahalli Bangalore

Generic Class

If a class type is not specific i.e this class will work for all type of data.

class ArrayList
{
Object obj;
 public void add(Object obj)
 {
 this.obj=obj;
  }
 public Object get(int index)
 {
  return obj;
 }
}

Normal class

class ArrayList
{
T obj;
 public void add(T obj)
 {
this.obj=obj;
  }
 public T get(int index)
 {
  return obj;
 }
}

Generic class

class ALGen
{
 T obj;
 void add(T obj)
 {
 this.obj=obj;
 } 
public T getObject()
 {
  return obj;
 }
}


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



class TestG3
{
public static void main(String args[])
{
ALGen l1=new ALGen();
l1.add("Dheeraj");
System.out.println(l1.getObject());

ALGen l2=new ALGen();
Employee e1=new Employee(1,"Atul",50);
l2.add(e1);
System.out.println(l2.getObject());
}
}

Footer with Map

Top