Java generics - bridge method -


when comes bridge method, know java compiler add them if there's need overriding can done subclass (after reading scjp mughal , angelikalanger website). bit confusing per below:

before erasure:

class x <t> {      void set(t t){}   }  class y <e> extends x {      void set(e e) {} // name clash here   }  class z<e> extends x {      void set(object y) {} // no name clash here   }  class z1<e> extends x<t> {      void set(object y) {} // name clash here   } 

after erasure:

class x {      void set (object t) {}   } 

i understand name clash class y why there no name clash class z? there name clash class z1? puzzling

class y <e> extends x {      void set(e e) {} // name clash here   } 

here name clash occurs because e not subclass of t. cannot override set method way.see explanation z1 understand better. class y work, must have

class y <e> extends x<e> {      void set(e e) {}   } 

next:

class z<e> extends x {      void set(object y) {} // no name clash here   } 

here there no name clash because in class x, set method gets interpreted as

void set(java.lang.object)  

and in class z parameter of set java.lang.object.so no clash.

next:

class z1<e> extends x<t> {      void set(object y) {} // name clash here   } 

again here name clash occurs because have have parameter of set whatever type parameter give x. here, pass x type parameter t, have parameter of set method java.lang.object. hence name clash.

for z work must have:

class z1<e> extends x<object> {          void set(object y) {}   } 

Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -