Skip to content
Advertisement

Unable to override generic method in java

abstract class Message <T> {
    abstract <T> void setContent(T content) ;
}
class TextMessage extends Message<String> {
     void setContent(String content) {
     }
}
public class Main
{
    public static void main(String[] args) {
        System.out.println("Hello World");
    }
}

I am getting errors like

Main.java:12: error: TextMessage is not abstract and does not override abstract method setContent(T) in Message class TextMessage extends Message { ^ where T is a type-variable: T extends Object declared in method setContent(T) 1 error

Advertisement

Answer

Your method declaration:

abstract <T> void setContent(T content) ;

hides the type parameter declared in class. That causes the error, most probably you don’t need that additional type parameter on the method.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement