Ufx
Ufx

Reputation: 2695

Java. Override method

public class MyMap extends LinkedHashMap<String, Serializable>
{
    @Override
    public Serializable get(String key)
    {
        return null;
    }
}

error: method does not override or implement a method from a supertype

Upvotes: 1

Views: 603

Answers (3)

NPE
NPE

Reputation: 500157

The method you're trying to override has the following signature:

public Serializable get(Object key);

To override it, your method's argument therefore has to be of type Object, not String:

public class MyMap extends LinkedHashMap<String, Serializable>
{
    @Override
    public Serializable get(Object key)
    {
        return null;
    }
}

Upvotes: 3

Tudor
Tudor

Reputation: 62439

The signature of get is public V get(Object key)

So you need to change the parameter type to Object instead of String.

Upvotes: 3

Pablo Santa Cruz
Pablo Santa Cruz

Reputation: 181270

Remove the @Override annotation. That will fix the error.

Keep in mind that if you actually want to override some parent method, this is not what you want to do. Instead, look for possible typos, error or type mismatch in your get method.

In your case, you probably want:

@Override
public Object get(Object key)
{
    return null;
}

Upvotes: 3

Related Questions