Generics

From Suhrid.net Wiki
Revision as of 03:05, 31 May 2011 by Suhridk (talk | contribs)
Jump to navigationJump to search
  • Generics is a way to enforce ONLY compile-time type safety.
  • All the type information is not present at run-time. The compiler strips out type information from the bytecode using a process called type erasure.
  • WHY Type erasure ? To ensure backward compatibility with legacy code.
  • This compile-time safety is broken when generic and non-generic legacy code are mixed up.
  • Watch out when autoboxing is involved with legacy code.
List l = new ArrayList();
l.add(123); //Auto-boxing happens.
int i = l.get(0); //Compile-time error. Autounboxing cant work because get() returns Object and not Integer.


Polymorphism and Generics

List<String> list = new ArrayList<String>();

Polymorphism only applies to the base type i.e list can be declared as arraylist You CANNOT do this:

 List<Animal> obj = new ArrayList<Dog>(); //NOT POSSIBLE

WHY ?

To prevent scenarios where you cannot add say, a Cat object to a Dog List. If the above conversion were possible it will be possible to do so. See below:

//NOTE : This is not possible actually, because the compiler prevents it.
public void foo() {
   List<Dog> dList = new ArrayList<Dog>();
   addAnimal(dList); //Compiler flags an error here. a Dog list cannot be assigned to an Animal list
}

private void addAnimal(List<Animal> aList) {
  aList.add(new Cat());
}