1
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

3 Answers 3

3

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;
}
Sign up to request clarification or add additional context in comments.

Comments

3

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

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

Comments

3

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;
    }
}

5 Comments

Why public V get(Object key) successfully overrides by @Override public Serializable put(String key, Serializable value) { } ?
@user1034253: I am sorry but I don't understand the question.
public Serializable put(String key, Serializable value) - compiled, public Serializable get(String key) as you say is need replace String by Object
@aix - OP's asking why put doesn't follow the same pattern. There's a popular post about this somewhere, we should just link it.
@user1034253 - See this post: stackoverflow.com/questions/857420/…

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.