I have the following scenario:
public class BodyPart {
abstract public void followBodyPart(BodyPart part);
}
public class Head extends BodyPart{
public void followBodyPart(Body body ) { //1.Why is this kind of implementation not allowed? 2. How is it called?
...
}
}
public class Body extends BodyPart{
public void followBodyPart(Head head ) {
...
}
public void followBodyPart(Forearm leftForearm ) {
...
}
...
}
//Arm, Forearm, etc...
The question is in the commented line above.
Now this is a small example and Body object can follow not only Head objects, but also Forearm and Leg objects, bigger examples could be more complex. 3. Why make the code more complicated with one function and lots of instanceof's, while java could theoretically allow us to break it into parts of few methods that overload each other and implement the abstract method?
The advantages are straightforward, if someone is using my library and I have lots of similar methods, first time user can be offered what to put in such a method (followBodyPart) from a class that inherits from BodyPart.
EDIT: The question as I meant it to be, regarding the design of Java does not belong to the begginers forum. See this question [here | http://stackoverflow.com/questions/11260426/why-is-the-following-interface-contract-not-allowed] .
Edited by: Vitali.pom on Jun 28, 2012 6:21 AM: Advantage of such "overriding" was added.
Edited by: Vitali.pom on Jun 28, 2012 7:47 AM: Added followBodyPart to Body and interface contract tag.
Edited by: Vitali.pom on Jun 28, 2012 7:55 AM: added "bigger examples could be more complex.".