Java generics - bridge method
NickName:yapkm01 Ask DateTime:2011-07-02T22:26:12

Java generics - bridge method

When comes to bridge method, i do know that java compiler will add them if there's a need so that overriding can be done properly by the subclass (after reading SCJP by mughal and angelikalanger website). But this is a bit confusing as 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 the name clash for class y but why there is no name clash for class z? Also there is a name clash for class z1? Puzzling

Copyright Notice:Content Author:「yapkm01」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/6557586/java-generics-bridge-method

Answers
aps 2011-07-02T14:45:35

class y <E> extends x { \n void set(E e) {} // name clash here \n}\n\n\nHere name clash occurs because E is not a subclass of T. So you cannot override the set method this way.see the explanation for z1 to understand better.\nFor class y to work, you must have\n\nclass y <E> extends x<E> { \n void set(E e) {} \n}\n\n\nNext:\n\nclass z<E> extends x { \n void set(Object y) {} // no name clash here \n}\n\n\nHere there is no name clash because in the class X, set method gets interpreted as\n\nvoid set(java.lang.Object) \n\n\nand in class z also the parameter of set is java.lang.Object.so no clash.\n\nNext:\n\nclass z1<E> extends x<T> { \n void set(Object y) {} // name clash here \n}\n\n\nAgain here name clash occurs because you have to have as parameter of set whatever type parameter you give to x. here, you pass to x type parameter T, but you have parameter of set method as java.lang.Object. hence name clash.\n\nFor z to work you must have:\n\nclass z1<E> extends x<Object> { \n void set(Object y) {} \n}\n",


More about “Java generics - bridge method” related questions

Java Generics - Bridge method?

Something called the "bridge method" concept related to Java Generics made me stop at a point and think over it. Btw, I only know that it occurs at the bytecode level and is not available ...

Show Detail

Java Generics - Bridge method?

Something called the "bridge method" concept related to Java Generics made me stop at a point and think over it. Btw, I only know that it occurs at the bytecode level and is not available ...

Show Detail

Java Generics - Bridge method?

Something called the "bridge method" concept related to Java Generics made me stop at a point and think over it. Btw, I only know that it occurs at the bytecode level and is not available ...

Show Detail

Java synthetic method and bridge method confusion

As stated in this Java Tutorial a bridge method implies that it is also a synthetic method. The question is, is it possible that invocation of isSynthetic method returns true but isBridge method re...

Show Detail

Why is this Java method reported as a bridge method?

Consider the following Java program: import java.lang.reflect.Method; public class BridgeTest { public static class ClassA { public void foo() { } } protected static c...

Show Detail

Java generics - bridge method

When comes to bridge method, i do know that java compiler will add them if there's a need so that overriding can be done properly by the subclass (after reading SCJP by mughal and angelikalanger we...

Show Detail

How to decompile the bridge method in generics?

In the generic section of 《Java Core Technology Volume 1》, the author mentions the decompilation results of the bridge method. However, the main test of jad, luyten and javap did not get the same r...

Show Detail

Generics Bridge Method --- on Polymorphism

I am trying to understand the concept of Bridge Method creation, and was stuck on the example given on the Oracle Java Docs. Below are the example for reference Given are the following two classe...

Show Detail

How to calculate bridge method target

Generics bring in a lot of goodness to java but they also mean trouble because of the introduction of bridge methods which mean its not easy to locate a method given a name and target because erasu...

Show Detail

How covarient return type is implemented using bridge method

I'm currently studying java generic by following the book "java generics and collection by Maurice Naftalin 2006". In the section covariant overriding on return type, the author stated Could so...

Show Detail