-3

I play Robocode (it is a java programming game). I want to change the method

public void onScannedRobot (ScannedRobotEvent e) {

}

to

public int onScannedRobot(ScannedRobotEvent e){
}

So I can adapt my robot more if it scanned is true or false.

I heard that it is possible with Overriding a method. So I tryed it:

I wrote:

public void onScannedRobot(ScannedRobotEvent e) {

    public int onScannedRobot(ScannedRobotEvent e) {
        return 1;
    }
}

But it won't work

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • You can't override a method with just a change of return value, but with a change to method parametres. So changing return type from `void` to `int` isn't enough, you have to change the `(ScannedRobotEvent e)`, either adding, removing or changing parametres. – AntonH Apr 19 '14 at 21:31
  • You're confusing overload and override here. Overrided methods must have the same argument list and they only allow covariant return. What you want is overload, but you can change the return type only if you change the parameters. http://docs.oracle.com/javase/tutorial/java/javaOO/methods.html – LMeyer Apr 19 '14 at 21:48

1 Answers1

0

This won't work because the return type of overriden method should be the same or a subtype of the return type declared in the original method. Look at this link about overriding and this about overloading

bartektartanus
  • 15,284
  • 6
  • 74
  • 102