Generic programming means to write code that can be reused for objects of many different types.
Before javaSE 5 generics was achieved with inheritance, afterwards to implement generics the compiler applies type Erasure
-bridge method to preserve polymorphism .
"assume a class or interface that extends a parameterized class, and I want to override a method that exist in the parameterized class, after erasure, the method signatures do not not match "the one in the parameterized class is replaced by their bounding type"
a Java compiler generates a bridge method to ensure that sub-typing works as expected
Before javaSE 5 generics was achieved with inheritance, afterwards to implement generics the compiler applies type Erasure
Erasure: "as if a correction mad by erasing" is the process of translating or rewriting code that uses generics into non-generic one, all info between angle bracket is erased "Type variables are erased & replaced by their bonding types 'Object for variables without bounds' ".NB:
-bridge method to preserve polymorphism .
"assume a class or interface that extends a parameterized class, and I want to override a method that exist in the parameterized class, after erasure, the method signatures do not not match "the one in the parameterized class is replaced by their bounding type"
different argument, different methods. to solvepublic void setXXX(Object x) //one for parameterized class "generic class" after erasure
public void setXXX(ClassType x) // my overridden method
a Java compiler generates a bridge method to ensure that sub-typing works as expected
public void setXXX(Object x){ setXXX( (ClassType) x);} //auto-generated by erasure
Comments
Post a Comment