Compilation problem with overriding method

Hi,

I have a class with these 2 methods:

    public Token clone() {
        return clone(this.getAnnotation(), null);
    }

    @Override
    public Token clone(Annotation annot, Map oAllreadyAddedAnnots) {
        return new Token(this, annot); //
    }

The superclass has the following method:

    public abstract AnnotationData clone(Annotation annot, Map oAllreadyAddedAnnots);

class Token is a subclass of AnnotationData.

This does not compile with IntelliJ Idea: the compiler complains, for method clone() in the subclass, that it returns AnnotationData instead of Token. Apparently, the compiler looks at the superclass method with the 2 arguments, not at the method in the subclass.
I agree this is a tricky case, but it does compile in the latest version of Eclipse, using a recent jdk 1.6.
Could IntelliJ Idea be adapted for compiling this? Thanks for your feedback.

3 comments

Hi Frederik,

Following code compiles fine for me. Testes under IJ 9.0.3 CE, jdk 1.6:

class AnnotationData {
}

class Token extends AnnotationData {
}

class Annotation {
}

abstract class Base {
    public abstract AnnotationData clone(Annotation annot, Map oAllreadyAddedAnnots);
}

class Sub extends Base {
     public Token clone() {
        return null;
    }

    @Override
    public Token clone(Annotation annot, Map oAllreadyAddedAnnots) {
        return null;
    }
}



Regards, Denis

0

Denis,

your code is not the same as mine. Does it still compile if you call the second clone method in the subclass from the first one? So:

class AnnotationData {
}

class Token extends AnnotationData {
}

class Annotation {
}

abstract class Base {
    public abstract AnnotationData clone(Annotation annot, Map oAllreadyAddedAnnots);
}

class Sub extends Base {
     public Token clone() {
        return this.clone(null, null);
     }

    @Override
    public Token clone(Annotation annot, Map oAllreadyAddedAnnots) {
        return null;
    }
}


This won't compile, and I think it should...

0

FrederikC wrote:

Denis,

your code is not the same as mine. Does it still compile if you call the second clone method in the subclass from the first one?
...

This won't compile, and I think it should...

Yes, it is compiled fine at IJ 9.0.3 CE, jdk 1.6.

Please provide information about your IntelliJ IDEA version that causes problems and try it with free Community Edition v.9.0.3.

Regards, Denis

0

Please sign in to leave a comment.