The Java Generic bridge approach

Source: Internet
Author: User

Feel the bridge method in the generic is difficult to understand, write a note (specifically, the core Java No. 536 page, my understanding may be biased).

As in the following two-segment code:

public class Pair<t> {private T first;private T second;public Pair () {first = null; second = NULL;} Public Pair (t first, T second) {this.first = first; this.second = Second;} Public T GetFirst () {return first;} Public T Getsecond () {return second;} public void Setfirst (T newvalue) {first = newvalue;} public void Setsecond (T newvalue) {second = newvalue;}}

Class DateInterval extends pair<date> {public void Setsecond (Date second) {}}

Due to the type erasure, DateInterval has two setsecond methods, respectively:

public void Setsecond (Date second)//self-created

public void Setsecond (Object second)//inherited from Pair<date>

Consider the following call:

DateInterval interval = new DateInterval (...);

Pair<date> pair = interval;

Pair.setsecond (adate);

Because of the polymorphism of the Java language, pair should theoretically invoke the Dateinterval.setsecond method. However, according to the above analysis, there are two setsecond methods in the DateInterval class, how to call it exactly?

The JVM works as follows:

1. The variable pair is declared as Pair<date> the type has only one Setsecond (object) method, and the virtual machine calls the Setsecond (object) method with the object referenced by the pair.

2. The object referenced by pair is DateInterval, so the Dateinterval.setsecond (object) method is called, and this method is the bridge method.

3. This bridge method invokes the Dateinterval.setsecond (Date) method.

Bridge method Code: public void Setsecond (Object second) {Setsecond ((Date) second);} Take a closer look at the date coercion type conversion inside the method, making the bridge method call the Dateinterval.setsecond (date) method

Bridge methods can sometimes become very strange, assuming that the DateInterval method also covers the Getsecond method:

Class DateInterval extends Pair<date> {public Date Getsecond () {return (date) Super.getsecond (). Clone ();}}

After erasing a type, DateInterval has two getsecond methods:

Date Getsecond ()//self-created

Object Getsecond ()//Inherit from pair<date>

Eh, is the signature of the method not a "method name + parameter"? Why is there a signature that returns a different type of method? In fact, in the JVM, the return type can be used to differentiate the method, and the virtual machine can handle the situation correctly.

The Java Generic bridge approach

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.