0

For all of those who aren't familiar with Greenfoot, below is the context:-

Greenfoot is a java learning tool using some animation. In this tool, there is a predefined class called 'Actor' with some predefined methods. We can add objects by creating sub-classes of this class such as 'Car', 'Truck', etc.

I created sub-classes to 'Actor' called 'Car' and 'Truck'. I called the predefined method in 'Actor' class called 'move(some argument denoting speed of the movement)' from a method in the 'Car' class as: move(5);.

My question is this: Why I don't need to mention the 'Car' class object here as in: c1.move(5); where 'c1' is the 'Car' class object? I can understand that since I didn't defined the 'move' method in the 'Car' class, it will directly call and implement the predefined method in the 'Actor' class, but how it is able to know that I meant 'Car' class object here? It could have been 'Truck' class object too! Is it because I am calling the method from a 'Car' class method, it is inferring that?

If yes, then is this a general rule in java or only a specific implementation of Greenfoot?

amsquareb
  • 148
  • 10
  • Where are you making the call to the move() method from?? – Ankit Khettry Jun 26 '16 at 08:16
  • Moreover, if you there is no move() method defined in either Car class or Truck class, it denotes that the same move() function defined inside Actor (which is inherited by Car and Truck classes) works for them both. If you want a separate implementation for either of the two class, you should define your own specific move() method in Car/Truck respectively. – Ankit Khettry Jun 26 '16 at 08:18
  • @AnkitKhettry From a method in the Car class. – amsquareb Jun 26 '16 at 08:20
  • If you are calling move() from another method in the Car class itself, it automatically automatically calls `this.move()`. 'this' denotes the current object upon which the current method in execution was called. – Ankit Khettry Jun 26 '16 at 08:22
  • 1
    @AnkitKhettry Thanks, you can write above down as answer if you wish, this resolved my query. – amsquareb Jun 26 '16 at 08:28
  • Glad I could help :) – Ankit Khettry Jun 26 '16 at 08:29

1 Answers1

0

Suppose in main(), you are creating an object of the Car class, and calling a method, say methodThatCallsMove():

Car c = new Car();
c.methodThatCallsMove();

Inside this method, you are calling move(5) simply:

methodThatCallsMove(){
  move(5);
}

This will automatically call the current object's (i.e. c) method move. The version of move() being bound to the object c here depends upon whether you have implemented a separate move() method in your Car class, or whether you are simply inheriting the original move() method in the Actor class. The former condition calls the move() implemented in your Car class, the latter will call the one in your Actor class.

Hope this helps.

Ankit Khettry
  • 997
  • 1
  • 13
  • 33
  • Can you please take a look at the question in the link:- [link](http://stackoverflow.com/q/38036106/6442608) – amsquareb Jun 27 '16 at 07:06