Path: csiph.com!aioe.org!.POSTED!not-for-mail From: =?UTF-8?Q?Arne_Vajh=c3=b8j?= Newsgroups: comp.lang.java.programmer Subject: Re: When to use dynamic binding over dynamic dispatch? Date: Sun, 8 May 2016 23:08:51 -0400 Organization: Aioe.org NNTP Server Lines: 74 Message-ID: References: NNTP-Posting-Host: ANasIS7Js9OyLQwgloeBLQ.user.gioia.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit X-Complaints-To: abuse@aioe.org User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.0 X-Notice: Filtered by postfilter v. 0.8.2 Xref: csiph.com comp.lang.java.programmer:34269 On 5/8/2016 4:30 PM, Robert Klemme wrote: > On 08.05.2016 16:37, Arne Vajhøj wrote: >> On 5/8/2016 8:57 AM, Eric Sosman wrote: >>> On 5/8/2016 8:23 AM, Arne Vajhøj wrote: >>>> On 5/8/2016 6:24 AM, Robert Klemme wrote: >>>>> On 04.05.2016 03:23, Arne Vajhøj wrote: >>>>>> All methods unless declared static or final are virtual in Java, >>>>>> so dynamic dispatch is used all the time. >>>>> >>>>> Methods declared as "final" are virtual as well. >>>> >>>> Given that: >>>> >>>> virtual method = method that can be overridden >>>> final method = method that can not be overridden >>>> >>>> then how can it be? >>> >>> class Super { >>> public void method() {} >>> } >>> >>> class Sub extends Super { >>> public final void method() {} >>> } >>> >>> ... >>> >>> Super obj = new Sub(); >>> obj.method(); >> >> As a starting point I would not call Sub.method >> a virtual method due to above definition. > > That seems to me a fairly uncommon definition. For me a virtual method > uses dynamic dispatch (late binding) as described in Wikipedia: > https://en.wikipedia.org/wiki/Virtual_method Well - it is actually mentioned as one possibility in the link you provide so ... >> But per JLS such calls are "invocation mode virtual". > > Hence it's a virtual method in my book. > >> If we make the logical conclusion that methods that will be called >> with invocation mode virtual are virtual methods, then the definition >> of virtual methods is certainly different in Java. > > Certainly different from the definition you offered above. Ironically > your first statement matches the definition of "virtual method" that I > have in mind much better because it speaks of "dynamic dispatch", i.e. > does not make a statement about whether it can be overridden and instead > focuses on invocation semantics. I just find it very difficult to see: public class Funny { public final void m() { System.out.println("I am funny"); } public static void main(String[] args) { Funny o = new Funny(); o.m(); } } as a virtual method. And there is no need for dynamic dispatch. Java just uses the same instruction as when dynamic dispatch is needed. Arne