DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > Java Auto-Synthesis

Java Auto-Synthesis

What is a bridge method and why does the compiler create it? Find out more in this article as we take a look at Java Generics.

Gourab Pattanayak user avatar by
Gourab Pattanayak
·
Mar. 24, 22 · Java Zone · Tutorial
Like (4)
Save
Tweet
2.73K Views

Join the DZone community and get the full member experience.

Join For Free
Java
 
class Base<T> {
	T obj;
	Base(T obj) {
		this.obj = obj;
	}
	T getob() {
		return obj;
	}
}

class Sub extends Base<String> {
	Sub(String str) {
		super(str);
	}
	String getob() {
		return obj;
	}
}


How many methods of the sub-class in the above code snippet will have after compilation, and why? Let’s make it a little bit simpler: How many methods the sub-class will have, excluding the methods inherited from java.lang.Object?

The answer is simple: Just modify the subclass, add a main method with the code snippet below, and execute. It will print all the methods and, of course, do not consider the main method.

Java
 
public class Sub extends Base<String> {
	Sub(String str) {
		super(str);
	}
	
  	@Override
  	String getob() {
		return obj;
  	}

  	public static void main(String args[]){
		for ( Method method : Sub.class.getDeclaredMethods()){
			System.out.println( “Method name:”+method.getName()+” | Return type :” +method.getReturnType());
		}
	}
}


Simple, right? But, wait: why am I seeing two methods for getob with two different return types “ java.lang.Object” and “java.lang.String”?

Method name: getob | Return type:class java.lang.Object
Method name: getob | Return type:class java.lang.String

We never defined a method getob() in our sub-class with a return type of java.lang.Object, which means  the compiler is creating it automatically...But why?

Well, let’s examine a little more before understating why. Modify the SOP statement to get some extra information about the methods.

Java
 
public static void main(String args[]){
 for ( Method method : Sub.class.getDeclaredMethods() ){
 	System.out.println( "Method name"+method.getName()+" | Return type :" +method.getReturnType()+" | isBridge :"+method.isBridge());
 }
}


Method name: getob | Return type:class java.lang.Object | isBridge :true

Method name: getob | Return type:class java.lang.String | isBridge :false

Now as we print the method meta-data, we can see that the additional method which was created by the compiler is a “bridge” method, (isBridge= true). What is a bridge method and why does the compiler create it?

The bridge method is an interesting topic and often not required to be known by Java developers because those are created and maintained by the compiler.

Here I will discuss one of the aspects of the bridge method related to our above example. In Java 5 with the addition of Generic for type-safety compilers have something called type “erasure." This is a process where the compiler removes (erase) all information related to “type parameters” and “type arguments” within a class or method for the sake of being binary compatible with Java libraries/applications that were created before generics.

If that’s the case, things are supposed to break here, because if we pass any other kind of object as a Generic parameter (say, Integer), the compiler has erased the type information already. However, it actually doesn’t because that’s where "Bridges" in Generics comes into the picture.

When the compiler translates the code for binary compatibility with older applications, it also adds the required bridge methods automatically in order to sustain the implementation contracts.

Note the first line of the output: we haven’t defined anywhere the return type as java.lang.Object in either subclass or superclass, but the magic is done by the compiler and added a bridge method with a return type of java.lang.Object. If we pass Integer or any other type as a type parameter to the sub-class it will still work because of the bridge.

There are also other scenarios where bridge methods are generated by the compiler. One such case is method overriding and co-variant return type.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Python 101: Equality vs. Identity
  • Correlation Between Fast TTM and Containers
  • Data Mesh — Graduating Your Data to Next Level
  • How to Get GDPR and Customer Communications Right

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo