0

When doing this, I get this error:

The return type is incompatible with MouseAdapter.mouseClicked(MouseEvent)

Class:

public class MyMouseAdapter extends MouseAdapter
{
    public MyMouseAdapter()
    {
        // TODO Auto-generated constructor stub
    }

    @Override
    public String mouseClicked(MouseEvent e)
    {
        // TODO Auto-generated method stub

    }
}

Where is that wrong? The original method is public void mouseClicked(MouseEvent e)

user207421
  • 305,947
  • 44
  • 307
  • 483
SegFault
  • 2,020
  • 4
  • 26
  • 41
  • The original return type is void and you changed it to String. At that point it is no longer an Override and since you have the @Override annotation, you get the error. – takendarkk Feb 08 '15 at 22:47
  • you try to return a String. it needs to return a void. (like in the superclass) – Stephen Buttolph Feb 08 '15 at 22:47
  • 2
    The OP will get an error whether there's an `@Override` annotation or not. You can't change the return type in Java (unless the return type is covariant). – markspace Feb 08 '15 at 22:49
  • Without the annotation the compiler sees that method as just any other random method. No error at all. – takendarkk Feb 08 '15 at 22:52
  • 2
    @HovercraftFullOfEels and @Takendarkk How should that work? The new method `public String mouseClicked(MouseEvent e)` has the same signature as the parents method `public void mouseClicked(MouseEvent e)`. The compiler also tells you that. – Tom Feb 08 '15 at 22:57
  • @Takendarkk No, I don't think so. Override/overload in Java depends solely on the method name and parameter signature, not the return type. So once the method name and parameter types match, it overrides, and then the return type must match (or be covariant). No return type match = error. But I didn't try it so... – markspace Feb 08 '15 at 23:02
  • @HovercraftFullOfEels Then you know more than Oracle: [The signature of the method declared above is: `calculateAnswer(double, int, double, double)`](http://docs.oracle.com/javase/tutorial/java/javaOO/methods.html). Or definition of a method signature changed in the last version(s). Also see this question: [Does a method's signature in Java include its return type?](http://stackoverflow.com/questions/16149285/does-a-methods-signature-in-java-include-its-return-type). – Tom Feb 08 '15 at 23:04
  • No, it isn't. See the very bottom of this page: http://docs.oracle.com/javase/tutorial/java/javaOO/methods.html – Kevin Krumwiede Feb 08 '15 at 23:05
  • 1
    Interesting about that is, that the JVM does include the return type as part of the signature: http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/types.html#wp276. But since @HovercraftFullOfEels said it would compile, he still is wrong at that point :P. But this helped us to learn something about method signatures and that the JVM "plays against the rules" :D. [Stackoverflow answer that mentions this detail](http://stackoverflow.com/questions/9909228/what-does-v-mean-in-a-class-signature#9909370). – Tom Feb 08 '15 at 23:14
  • 1
    I'm very very happy someone corrected me/us. I was dead set on it being a part of the signature. My major lesson for the day. – takendarkk Feb 08 '15 at 23:18
  • @HovercraftFullOfEels Well since, you're correct in 99,9% of your answers/comments, this little "thing" doesn't change much. Glad you're active on SO :). – Tom Feb 08 '15 at 23:20
  • @Tom: and a big ditto back at you. And thanks to you and EJP for keeping us all fully honest. – Hovercraft Full Of Eels Feb 08 '15 at 23:21

1 Answers1

5

When overriding a method, it must be done in a way that the subclass absolutely represents it's parent. In this case, you're overriding mouseClicked(MouseEvent e), which isn't allowed return anything; it's a void method. So, firstly, to get around this problem you need to change your implementation to:

public void mouseClicked(MouseEvent e) {
     /** Do stuff. **/
}

The reason you're not allowed to change the return type of the method is because when subclassing a parent class, you're saying that the subclass can be interacted with in the exact same way as it's parent. So, if you had an array of objects which all inherit from the same parent, you can treat them all in this generic fashion; you know they all return no data when their mouseClick method is called.

Hypothetically speaking, if some implementations of this class returned a String when the mouse is clicked, and some didn't, how would a generic interaction with the array of these subclasses be able to tell the difference? This is where the power of Object Oriented programming comes into play; you can interact with instances of a MouseAdapter in a generic fashion, and allow them to override this method in their own class-specific way.

You can work around this by adding some methods to the class which can be called from the mouseClicked(MouseEvent e) method, which would allow you to handle your String data. This would ensure interaction with your subclass in a specific way, after handling the generic input event.

Mapsy
  • 4,192
  • 1
  • 37
  • 43