Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!weretis.net!feeder4.news.weretis.net!newsfeed.straub-nv.de!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail From: Jim Janney Newsgroups: comp.lang.java.programmer Subject: Re: calling own methods from constructor Date: Thu, 07 Apr 2011 16:15:23 -0600 Organization: As little as possible Lines: 44 Message-ID: <2pwrj56744.fsf@shell.xmission.com> References: <2011040622233261380-angrybaldguy@gmailcom> <2pzko2f2nx.fsf@shell.xmission.com> Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit Injection-Info: mx03.eternal-september.org; posting-host="PnllQd880uOddfy6hsxHuQ"; logging-data="8908"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+FjmLWp+pvVCgD6CZTHecQ" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:evE6JVEZkbNHop0OylFLNF0BCkM= sha1:xWkgOEzMHe8F0VSAGJD2UxnGsqY= Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:2953 Lew writes: > Janney wrote: >> In Java evaluating new Bar() will throw an exception.  But in C++ the >> equivalent code would print >> >> Override me! I dare you. >> Two guys walk into a... >> >> In effect, until the constructor of Foo completes, the object is >> considered to be an instance of Foo, so calls to virtualMethod() to to >> Foo.virtualMethod even if it has been overridden.  After the super >> constructor completes, the object is treated as an instance of Bar, so >> evaluating new Bar().virtualMethod() would print two lines and then >> throw an exception. >> >> I've been surprised by this behaviour in C++ enough times that I'm not >> sure that it has the better approach.  But a solution does exist. >> > > A solution to what, exactly? In the message I was replying to, in the text that you deleted, Owen Jacobsen correctly observed that >>> It's hard to prove that a constructor never calls a virtual method. Consider: This is a technical issue, and I observed that other languages have found ways to prevent virtual methods from being called before their owning objects have been fully constructed. One can debate whether this desirable, but C++ provides an existence proof that it's possible. > I ask because it sort of sounds like you're saying that C++ is a > solution to something in Java. I don't guess that's what you're > really saying, of course, but it leaves me wondering what that is. > What is the problem that you aver exists, and what is its solution to > it that you aver exists? Consider focussing on what I did say, instead of what it sort of sounds like I might have said. The solution is in the paragraph above, the one that starts with "In effect, until the constructor of Foo completes". -- Jim Janney