Path: csiph.com!usenet.pasdenom.info!news.albasani.net!eternal-september.org!feeder.eternal-september.org!mx04.eternal-september.org!.POSTED!not-for-mail From: Joshua Cranmer Newsgroups: comp.lang.java.programmer Subject: Re: multiple inheritance Date: Wed, 01 Aug 2012 23:41:50 -0400 Organization: A noiseless patient Spider Lines: 31 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Thu, 2 Aug 2012 03:42:01 +0000 (UTC) Injection-Info: mx04.eternal-september.org; posting-host="55bb637b225321a57418a16daf2e1903"; logging-data="16364"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19w5fktXpiFTlTleTKRqyq4pJ0ikgvWKF8=" User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:14.0) Gecko/20120713 Thunderbird/14.0 In-Reply-To: Cancel-Lock: sha1:Uxps3XKq7eo6YqA0goOcc9IbEb0= Xref: csiph.com comp.lang.java.programmer:16948 On 8/1/2012 10:28 PM, bob smith wrote: > Why doesn't Java support multiple inheritance? Because multiple inheritance is really, really, really complicated and confusing for most users. The short answer is the diamond problem: class A { int varA; }; class B : A { int varB; }; class C : A { int varC; }; class D : B, C { int varD; }; There are two main points of contention in this kind of hierarchy: 1. How many copies of varA should D have? Intuitively, one is probably what most people would expect, but the implementations of B and C would have to cooperate in realizing that their superclass may be shared with D. It also incurs a penalty in runtime costs 2. How does initialization/override order get resolved? Is it "BFS"-y (like D, B, C, A) or "DFS"-y (D, B, A, C)? There are even more convoluted orders in practice (C3 appears to be the most common nowadays), but this is the sort of stuff that tends to cause nasty sorts of little edge cases in practice. It is rare in practice that you need true multiple inheritance, in the sense of inheritance of implementation; multiple inheritance of interface is common, and this is as far as Java goes. -- Beware of bugs in the above code; I have only proved it correct, not tried it. -- Donald E. Knuth