JVM Knowledge

Two methods or constructors, M and N, have the same signature if they have the same name, the same type parameters (if any) (§8.4.4), and, after adapting the formal parameter types of N to the type parameters of M, the same formal parameter types.

The signature of a method m_1 is a subsignature of the signature of a method m_2 if either:

  • m_2 has the same signature as m_1, or
  • the signature of m_1 is the same as the erasure (§4.6) of the signature of m_2.

Two method signatures m_1 and m_2 are override-equivalent iff either m_1 is a subsignature of m_2 or m_2 is a subsignature of m_1.

It is a compile-time error to declare two methods with override-equivalent signatures in a class.

Example 8.4.2-1. Override-Equivalent Signatures

class Point {
    int x, y;
    abstract void move(int dx, int dy);
    void move(int dx, int dy) { x += dx; y += dy; }
}

This program causes a compile-time error because it declares two move methods with the same (and hence, override-equivalent) signature. This is an error even though one of the declarations is abstract.

The notion of subsignature is designed to express a relationship between two methods whose signatures are not identical, but in which one may override the other. Specifically, it allows a method whose signature does not use generic types to override any generified version of that method. This is important so that library designers may freely generify methods independently of clients that define subclasses or subinterfaces of the library.

Consider the example:

class CollectionConverter {
    List toList(Collection c) {...}
}
class Overrider extends CollectionConverter {
    List toList(Collection c) {...}
}

Now, assume this code was written before the introduction of generics, and now the author of class CollectionConverter decides to generify the code, thus:

class CollectionConverter {
    <T> List<T> toList(Collection<T> c) {...}
}

Without special dispensation, Overrider.toList would no longer override CollectionConverter.toList. Instead, the code would be illegal. This would significantly inhibit the use of generics, since library writers would hesitate to migrate existing code.