Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail From: Lew Newsgroups: comp.lang.java.programmer Subject: Re: multiple inheritance Date: Thu, 2 Aug 2012 01:05:44 -0700 (PDT) Organization: http://groups.google.com Lines: 47 Message-ID: <7bed39a6-28cd-4edb-8425-84bd6883ea0e@googlegroups.com> References: NNTP-Posting-Host: 173.164.137.214 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 X-Trace: posting.google.com 1343894747 18251 127.0.0.1 (2 Aug 2012 08:05:47 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Thu, 2 Aug 2012 08:05:47 +0000 (UTC) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=173.164.137.214; posting-account=CP-lKQoAAAAGtB5diOuGlDQk0jIwmH0T User-Agent: G2/1.0 Xref: csiph.com comp.lang.java.programmer:16952 Joshua Cranmer wrote: > bob smith wrote: >> Why doesn't Java support multiple inheritance? Strictly speaking, Java does support multiple inheritance, just not from classes. This is because multiple inheritance of implementation is silly. > 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. Quite so. -- Lew