9

In JLS 8, Section 8.4.8.1 there is a statement:

A concrete method in a generic superclass C can, under certain parameterizations, have the same signature as an abstract method in that class. In this case, the concrete method is inherited and the abstract method is not. The inherited method should then be considered to override its abstract peer from C.

Could anyone provide an example of such parametrization for generic class? I was not able to.

1
  • 1
    It's on page 244 in the bottom. Provided quote is slightly modified to make it shorter.
    – nezdolik
    Jun 8, 2016 at 11:52

2 Answers 2

2

Maybe

public abstract class A<T> {
    public abstract void m(T t);

    public void m(String s) {}
}

public class B extends A<String> {
}

In this case both methods in B will be void m(String).

1
  • this looks like an appropriate example, i tried more complex ones and they did not worked :) thanks!
    – nezdolik
    Jun 8, 2016 at 12:29
2

The above answer is correct given by @Roman, I want to add one more thing to that answer. If we change the parameter of method m() from String to Object, then there will be compilation error, because Generics works on Type erasure, so after type erasure m(T t) -> will be to m(Object t), which is compilation error because we cannot have two methods with same name and signature. See below compilation error -

public abstract class A<T> {
    public abstract void m(T t); // compilation error: m(T) and m(Object), both method have same erasure

    public void m(Object s) {}
}

public class B extends A<Object> {
     @Override
    public void m(Object o) {
    }
}

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.