Java Overriding Generic Methods

Frank :

How come this compiles:

interface Test {
    <T extends Integer> T getValue(T n);
}

class Impl implements Test{
    public Integer getValue(Integer n) {
        return n;
    }
}

But this doesn't:

interface Test {
    Integer getValue(Integer n);
}

class Impl implements Test{
    public <T extends Integer> T getValue(T n){
        return n;
    }
}

It gives me the following compile error:

Impl is not abstract and does not override abstract method getValue(Integer) in Test

error: name clash: getValue(T) in Impl and getValue(Integer) in Test have the same erasure, yet neither overrides the other

Doesn't erasure ensure that T gets replaced with Integer? So why would the second example not be valid?

Brian Goetz :

You are allowed to override a generic method with a non-generic one whose signature is the erasure of the generic method, but not the other way around. There's a good reason for this -- to allow supertypes to migrate to generics without forcing all the subclasses to migrate on the same day. So the compiler is tolerant of erased overrides. In the other direction, though, you are trying to override a non-generic method with a generic one, and there's no reasonable migration scenario that could produce that -- it's just an error.

The same is true for classes; you can write

class Foo<T> { }
class Bar extends Foo { }

but not

class Foo { }
class Bar extends Foo<T> { }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Proper way of overriding generic methods in Java

From Dev

Java Generics: Overriding generic methods, wildcard shorthand?

From Java

Overriding generic methods with non-generic implementations

From Java

Overriding synchronized methods in Java

From Java

Overriding Methods constraints in java

From Java

Overriding protected methods in Java

From Dev

overriding methods in Interface java

From Dev

Overriding generic methods from base class

From Java

Overriding a method with Generic Parameters in Java?

From Java

Java erasure with generic overloading (not overriding)

From Dev

Java Generic Class Method Overriding

From Dev

Is Java prevent overriding static methods?

From Dev

Binding in Java (overriding methods and "fields")

From Dev

Java static methods vs overriding

From Java

Java: Generic methods and numbers

From Java

Invoking Java Generic Methods

From Java

Java Extending Java Classes and Overriding Their Methods

From Dev

java generic inheritance. overloading or overriding

From Dev

Java: Overriding Generic Method with Typed Method?

From Java

Decorator design pattern java overriding methods question

From Java

Actionscript overriding methods in extended interfaces vs Java?

From Java

Java Interface: Inheriting, Overriding, and Overloading Methods

From Java

Is "Indirect Overriding" of static methods possible in Java?

From Dev

“overriding” private methods with upcasting call in java

From Dev

What classes in java are overriding equals() and hashCode() methods?

From Java

Java - generic classes hierarchy and generic methods overloading

From Dev

Java overloading methods with generic and a collection of a generic

From Java

Java generic methods in generics classes

From Java

Return Type of Java Generic Methods

Related Related

  1. 1

    Proper way of overriding generic methods in Java

  2. 2

    Java Generics: Overriding generic methods, wildcard shorthand?

  3. 3

    Overriding generic methods with non-generic implementations

  4. 4

    Overriding synchronized methods in Java

  5. 5

    Overriding Methods constraints in java

  6. 6

    Overriding protected methods in Java

  7. 7

    overriding methods in Interface java

  8. 8

    Overriding generic methods from base class

  9. 9

    Overriding a method with Generic Parameters in Java?

  10. 10

    Java erasure with generic overloading (not overriding)

  11. 11

    Java Generic Class Method Overriding

  12. 12

    Is Java prevent overriding static methods?

  13. 13

    Binding in Java (overriding methods and "fields")

  14. 14

    Java static methods vs overriding

  15. 15

    Java: Generic methods and numbers

  16. 16

    Invoking Java Generic Methods

  17. 17

    Java Extending Java Classes and Overriding Their Methods

  18. 18

    java generic inheritance. overloading or overriding

  19. 19

    Java: Overriding Generic Method with Typed Method?

  20. 20

    Decorator design pattern java overriding methods question

  21. 21

    Actionscript overriding methods in extended interfaces vs Java?

  22. 22

    Java Interface: Inheriting, Overriding, and Overloading Methods

  23. 23

    Is "Indirect Overriding" of static methods possible in Java?

  24. 24

    “overriding” private methods with upcasting call in java

  25. 25

    What classes in java are overriding equals() and hashCode() methods?

  26. 26

    Java - generic classes hierarchy and generic methods overloading

  27. 27

    Java overloading methods with generic and a collection of a generic

  28. 28

    Java generic methods in generics classes

  29. 29

    Return Type of Java Generic Methods

HotTag

Archive