Forum Stats

  • 3,872,935 Users
  • 2,266,490 Discussions
  • 7,911,389 Comments

Discussions

How this is called and why it is not allowed?

User_WZNFN
User_WZNFN Member Posts: 13 Blue Ribbon
edited Jun 29, 2012 4:28PM in New To Java
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.".
Tagged:
«1

Answers

  • EJP
    EJP Member Posts: 32,922 Gold Crown
    1.Why is this kind of implementation not allowed?
    It is allowed. However it isn't allowed as an implementation of the interface method, because Body is not the same as BodyPart.
    2. How is it called?
    It's called method overloading. Defining the same method name with more than one distinct argument signature.
    Now this is a small example and Body object can follow not only Head objects, but also Forearm and Leg objects.
    I don't know what 'follow' means, but you haven't shown any type-relationship of Body to anything else, let alone its identity with BodyPart, so the code you posted won't compile.
    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?
    Because the compiler doesn't know what you're talking about, any more than I do, because you haven't provided any information whatsoever to justify the compiler accepting Body where you specified BodyPart.

    To support your claim what you propose is 'theoretically possible', you would need to start by trying to draw up the inference rules that would be required to implement it; bearing in mind that the Java rules for method matching are already complex enough to be written out in type-algebra, and that whatever you come up with has to be 100% compatible with the existing rules. Good luck.
  • gimbal2
    gimbal2 Member Posts: 11,949 Gold Trophy
    edited Jun 28, 2012 10:10AM
    EDIT: ninja'd by EJP, adapting.

    "Interface contract" is probably another term you're looking for. And what you want can be achieved by applying generics, but not in the way you have chosen to be the only correct way of getting the result you want.
    gimbal2
  • User_WZNFN
    User_WZNFN Member Posts: 13 Blue Ribbon
    edited Jun 28, 2012 11:10AM
    Thanks to both of you.
    EJP, maybe I missed a bit the forum, since this is not for begginers except for the question regarding the name, but I meant why essentially, when Java was built, why this kind of implementation was not allowed? Nor in its' updated version. However I do understand how does Java compiler work.
    I searched googled for "interface contract" and didn't find anything that matches what I described since it is a more general term which is related to interfaces, if it doesn't exist in any other language, than it's clear why there is no name for it.
    Implementing with generics might be interesting, but this piece of code is supposed to be ported to Java ME, in addition to Java SE.
  • gimbal2
    gimbal2 Member Posts: 11,949 Gold Trophy
    edited Jun 28, 2012 11:27AM
    Vitali.pom wrote:
    when Java was built, why this kind of implementation was not allowed?
    You'd have to ask the original designers of Java about what they were thinking more than 15 years ago.

    Lets say I am one of them; what will you be doing with the information? Say I can tell you WHY!? WHY!? - what then? Does it help you?


    You'll learn about interfaces and contracts some day, I'm sure of it.
  • jschellSomeoneStoleMyAlias
    jschellSomeoneStoleMyAlias Member Posts: 24,877 Gold Badge
    Vitali.pom wrote:
    //1.Why is this kind of implementation not allowed?
    I am rather certain it would make class verification impossible.
    BodyPart body= new Body();
    BodyPart bp = new Body();
    Doit(body, other)
    
    void Doit(BodyPart bp, BodyPart other)
       {
       bp.followBodyPart(other); // Add Body to Body even though it expects head
       }
  • User_WZNFN
    User_WZNFN Member Posts: 13 Blue Ribbon
    edited Jun 29, 2012 3:11AM
    Few notes about your message, first of all Body's follow method expect not only Head ,but also Forearm... But this is irrelevant.
    Anyway, trying to put Body where Head is expected is what I'm trying to avoid with what I'm offering, and not only to solve it, but to solve it during pre-compilation time and let the IDE offer you what to put in the function. No, there won't be problems with class verification.
  • Carlo A
    Carlo A Member Posts: 18
    Hello, i don't understend exactly what type of error/warning do you want to catch at "pre-compile time" (i suppose you use an IDE like Eclipse or Netbeans) but really do you need the followBodyPart(BodyPart part) method in the abstract class?
    If you have such a method in the abstract class you cannot catch warning at compile time but only at runtime putting the right tests (using for example instancof) in each implementation of the followBodyPart(BodyPart part) methods.

    In the following code IDE give you an error if you try, for example, to make a Body follow another Body
    public class BodyTest {
    
    	public static void main(String[] args) {
    		Head h = new Head();
    		Body b = new Body();
    		h.followBodyPart(b);
    		b.followBodyPart(h);
    		b.followBodyPart(b);	// The method followBodyPart(Head) in the type Body is not applicable for the arguments (Body)
    	}
    }
    
    abstract class BodyPart {
    }
    
    class Head extends BodyPart {
    	public void followBodyPart(Body body) {
    		System.out.println("followBodyPart(Body body) of class: '" + getClass() + "', '" + body.getClass() + "'");
    	}
    }
    
    
    class Body extends BodyPart {
    	public void followBodyPart(Head head) {
    		System.out.println("followBodyPart(Head head) of class: '" + getClass() + "', '" + head.getClass() + "'");
    	}
    }
    Carlo
  • User_WZNFN
    User_WZNFN Member Posts: 13 Blue Ribbon
    I copied the code from my original code and that's why BodyPart was abstract, I've just changed it in the post.
    I'm also adding the note about the wrong forum in my first message, please read it.
  • Carlo A
    Carlo A Member Posts: 18
    If BodyPart class isn't abstract you cannot define an abstract method in it. You don't answer anything about my previous post.

    Carlo
  • User_WZNFN
    User_WZNFN Member Posts: 13 Blue Ribbon
    You're right, but what I'm complaining about is not what you mentioned, but the fact that the "IDE" does not allow to implement followBody in the following way:
    class Head extends BodyPart {
    	public void followBodyPart(Body body) {
    		System.out.println("followBodyPart(Body body) of class: '" + getClass() + "', '" + body.getClass() + "'");
    	}
    }
    Why does BodyPart need this abstract method is another question which does belong to this forum and deserves another topic.
This discussion has been closed.