Brian Frank wrote: > In Fantom, I have been using invokespecial as poor-man's non-virtual > dispatch to avoid method naming conflicts between modules. But it > appears the Andriod's VM requires the ACC_SUPER flag and won't let you > call a method unless it is actually marked as private. So I need to > switch to invokevirtual, but still want to avoid that subclasses can > accidentally override a module scoped method in a base class. > > Experimenting with Java, it appears that the JVM dispatches > invokevirtual correctly in the case of package scoped methods. That is > given these two classes: > > package alpha; > public class A { String foo() { return "A.foo"; } > > package beta; > public class B extends A { String foo() { return "B.foo"; } > > It seems that calling A.foo on an instance of B will always correctly > dispatch to A.foo (B's version does not truly override A.foo). I tried > to find where this behavior is dictated by the JVM language spec, but > couldn't find any mention of it. > > Is this the specified behavior that can be counted on across different > JVMs?
I don't know the answer, but I had to reply because this is one of my (many) pet peeves about the JVM spec. It used to be the case that method dispatching was completely unspecified in the JVM spec (I think it, implicitly, delegated this to the JLS, which is completely unacceptable IMNSHO). I seem to recall that recently there have been some efforts to remove the JLS dependency from the JVM spec, but I don't know what became of that. I just tried googling the 3rd edition of JVM spec, but couldn't find anything recent. Also, because of the lack of specification, the behavior is sometimes a bit odd. For example, if you modify your example so that A has a base class (Base) with a public foo method, now A.foo will override Base.foo and B.foo will override A.foo. Regards, Jeroen -- You received this message because you are subscribed to the Google Groups "JVM Languages" group. To post to this group, send email to jvm-languages@googlegroups.com. To unsubscribe from this group, send email to jvm-languages+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/jvm-languages?hl=en.