Search code examples
javagenericsinterfaceoverridingabstract-class

Using generics as method parameter Java


I have an interface :

    public interface DDLOperation <T extends Artifact> {

    public void alter(T artifact);

    public void create(T artifact);

    public void drop(T artifact);

} 

I also have an abstract class implementing that interface :

    public abstract class AbstractDDLOperation <T extends Artifact> implements DDLOperation {

    @Override
    public abstract void alter(T artifact);

    @Override
    public abstract void create(T artifact);

    @Override
    public abstract void drop(T artifact);

    public void processChildElement(Element childElement) {
        Artifact artifact = createArtifact(childElement);
        alter(artifact);
    }
}

Eclipse gives this error in AbstractDDLOperation class : The method alter(T) of type AbstractDDLOperation must override or implement a supertype method

I want all subclasses implementing AbstractDDLOperation to pass different type of parameters, all extending from Artifact class to the methods alter, create and drop.

Can anyone give a suggestion about the issue?

Thanks.


Solution

  • Your class should implement DDLOperation<T>, not the raw DDLOperation.

    It should be:

    public abstract class AbstractDDLOperation <T extends Artifact> implements DDLOperation<T>
    

    Otherwise, your abstract class's methods don't override or implement any of the super type (interface) methods, so the @Override annotation is incorrect.