• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

What are bridge methods in Java?

 
Ranch Hand
Posts: 598
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got this question from javaBlackBelt.

Which of the following statements are true about bridge methods in Java 5?

They are synthetic
They are generated by the compiler
They are not necessary if the code does not use generics
The compiler rejects code that directly calls a bridge method
It is possible to call a bridge method using reflection



Explanation
Bridge methods are inserted by the compiler to ensure correct overriding. They are sometimes necessary when overriding generic types and they are necessary for covariant method return types. So bridge methods are not limited to code using generics. A bridge method is synthetic and cannot be called directly but it can be called using reflection. Of course they should not be called and doing so may result in error, e.g. a ClassCastException, when calling a bridge method with wrong parameter types.
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well this is a deep topic and I don't think that it is a part of SCJP.

But to explain, let's take an example of covarient return types.



Now you must say that method() in Derived overrides method() in Base. But this is not the case. Actually return types are also part of method signature for JVM. So for JVM these two methods are overloaded. To solve this problem, compiler adds a bridge method in Derived class which calls the covarient overloaded method. I don't remember how that method is implemented by compiler...
 
Himanshu Gupta
Ranch Hand
Posts: 598
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can i get some more links as I searched but was not able to find some good articles.

If there is someone who knows it better can explain here for us.

Thanks in advance.
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ankit is corect, this is not on the exam, so let's move this discussion...
 
I don't even know how to spell CIA. But this tiny ad does:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic