Jay
Jay

Reputation: 691

How to override a generic method

Generic method :

public <T> void foo(T t);

Desired overridden method :

public void foo(MyType t);

What is the java syntax to achieve this?

Upvotes: 0

Views: 416

Answers (3)

Upio
Upio

Reputation: 1374

A better design is.

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

class Impl implements Generic<MyType> {
    @Override
    public void foo(MyType t) { }
}

Upvotes: 2

Snehal Masne
Snehal Masne

Reputation: 3429

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

Upvotes: 2

talex
talex

Reputation: 20542

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

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

    }
}

Upvotes: 0

Related Questions