0

Generic method :

public <T> void foo(T t);

Desired overridden method :

public void foo(MyType t);

What is the java syntax to achieve this?

Jay
  • 671
  • 9
  • 10
  • Is T constrained by the class? As in, is the method enclosed in some generic `class Example { }` – Upio Sep 04 '14 at 09:35

3 Answers3

2

You might want to do something like this :

abstract class Parent {

    public abstract <T extends Object> void foo(T t);

}

public class Implementor extends Parent {

    @Override
    public <MyType> void foo(MyType t) {

    }
}

A similar question was answered here as well : Java generic method inheritance and override rules

Community
  • 1
  • 1
Snehal Masne
  • 3,403
  • 3
  • 31
  • 51
  • Do you understand that `MyType` here is generic parameter name, not name of existing class? – talex Sep 04 '14 at 09:41
2

A better design is.

interface Generic<T> {
    void foo(T t);
}

class Impl implements Generic<MyType> {
    @Override
    public void foo(MyType t) { }
}
Upio
  • 1,364
  • 1
  • 12
  • 27
0
interface Base {
    public <T> void foo(T t);
}

class Derived implements Base {
    public <T> void foo(T t){

    }
}
talex
  • 17,973
  • 3
  • 29
  • 66