| Author |
To the author: Multiple Inheritance
|
Nicholas Cheung
Ranch Hand
Joined: Nov 07, 2003
Posts: 4982
|
|
Dear Cay and Gary, Up till now, Java does not allow multiple inheritance, however, I sometimes really find that it is necessary, otherwise, our coding might have duplications. Do you think Java, say, in next core release, will allow us to do so? In addition, what is your view on this issue? Nick
|
SCJP 1.2, OCP 9i DBA, SCWCD 1.3, SCJP 1.4 (SAI), SCJD 1.4, SCWCD 1.4 (Beta), ICED (IBM 287, IBM 484, IBM 486), SCMAD 1.0 (Beta), SCBCD 1.3, ICSD (IBM 288), ICDBA (IBM 700, IBM 701), SCDJWS, ICSD (IBM 348), OCP 10g DBA (Beta), SCJP 5.0 (Beta), SCJA 1.0 (Beta), MCP(70-270), SCBCD 5.0 (Beta), SCJP 6.0, SCEA for JEE5 (in progress)
|
 |
Alvin chew
Ranch Hand
Joined: Jan 08, 2004
Posts: 834
|
|
|
hi, nick, why you feel implement interfaces is not good enough to handle multiple inheritance ?
|
 |
Nicholas Cheung
Ranch Hand
Joined: Nov 07, 2003
Posts: 4982
|
|
Because we certain need 2 sets of functions, which have been implemented already. If we just let our class to implement the interfaces, each class that require those methods will need to implement themselves. But how about, if the implementation in fact could be the same? Nick
|
 |
Cay Horstmann
author
Ranch Hand
Joined: Nov 14, 2004
Posts: 77
|
|
I am quite sure that Java will never have multiple inheritance. Indeed, sometimes, it is a pain not to have it. But implementing multiple inheritance is very hard. In C++, you have messy situations with virtual base classes. In Eiffel, you avoid all that, but the implementation cost is huge, particularly in deep inheritance hierarchies. Some people think that multiple inheritance is a completely academic issue that never comes up in real programming. But sometimes it does. Here is an example. Remember good old AWT? There are Component objects and Container objects. A Container can hold Component objects. (The "Composite" pattern at work...) Then Swing came along. JComponent extends Component. Oops...it doesn't. It actually extends Container because SOME Swing components (such as JPanel) are containers. In C++, we could have JContainer extend JComponent and Container (well, maybe, if all the virtual base classes are properly defined...), but Java has no multiple inheritance. Therefore, JComponent and its subclasses such as JButton inherit unsightly methods "add" and "remove", even though it makes no sense to add a component to a JButton. Maybe someone will figure out how to implement multiple inheritance efficiently, but for now, I think the Java compromise of "single superclass, multiple interfaces" is the best deal in town. Cheers, Cay
|
Author of <a href="http://www.amazon.com/exec/obidos/ASIN/0131482025/ref=jranch-20" target="_blank" rel="nofollow">Core Java 2, Volume I - Fundamentals (7th Edition)</a>
|
 |
Nicholas Cheung
Ranch Hand
Joined: Nov 07, 2003
Posts: 4982
|
|
Maybe someone will figure out how to implement multiple inheritance efficiently, but for now, I think the Java compromise of "single superclass, multiple interfaces" is the best deal in town.
I agree that mutliple inheritance really gave us troubles in old C++ day, however, sometimes, as your example, we really need to have this. Otherwise, we might either (i) merge the related classes to avoid the inheritance; (ii) duplicate the codes in different subclasses. In case we really have this situation (which I am facing now), which way you would suggest? Nick
|
 |
 |
|
|
subject: To the author: Multiple Inheritance
|
|
|