Do not all methods of the parent class use bridge methods when referring to an object of the inherited class?

  • 0
    Good day. I read Horstmann's chapter on generalizations, it talked about how bridge methods work for generalized methods.
    (class Pair is just a generic class with two variable fields)

    class Datelnterval extends Pair<Date>
      public void setSecond(Date second){
        if (second.compareTo(getFirst()) >= 0)
    • • •
    public static void main(String[] args){
      Datelnterval interval = new Datelnterval(. . .);
      Pair<Date> pair = interval; // допускается присваивание суперклассу
      pair.setSecond(aDate) ;

    The setSecond () method call is assumed to be polymorphic, so
    the corresponding method is called. And since the variable pair is referenced
    to an object of type Datelnterval, it must be a call to Datelnterval. setSecond ().
    But the fact is that type erasure interferes with the principle of polymorphism.
    As a way out of this predicament, the compiler generates the following
    blowing bridge method in Datelnterval class:
    public void setSecond (Object second) {setSecond ((Date) second); }
    In order to make this mechanism clearer, let's analyze the execution when
    the operator below.
    pair.setSecond (aDate)
    In the declaration of the variable pair, the type Pair is specified, and this type refers to
    There is only one method called setSecond, namely setSecond (Object).
    The virtual machine calls this method on the object that is being referenced.
    variable pair. This object is of type Datelnterval, and therefore calling
    method Datelnterval. setSecond (Object). It is he who is synthesized.
    ny bridge method. After all, it, in turn, calls the Datelnterval method.
    setSecond (Date), which is exactly what is required.

    I would like to know if, having a parent class reference to the inheritor object, where both are without class variables, the same procedure is not performed (obviously, it is not, since these very bridge methods are described in this chapter)?
    < br /> I apologize if the question is posed incorrectly, the structures being operated seem rather confusing.
    Java Anonymous, May 29, 2020

  • 0 Answers
Your Answer
To place the code, please use CodePen or similar tool. Thanks you!