主题:【原创】JAVA和C#,武当和少林之争! -- Highway
Q:Why doesn't the Java language support multiple inheritance?
A: Whenever you find yourself asking why Java has or does not have some feature, consider the design goals behind the Java language. With that in mind, I started my search by skimming through "The Java Language Environment" by James Gosling and Henry McGilton (Sun Microsystems), a white paper published in May 1996 that explains some of the reasoning behind Java's design.
As the white paper states, the Java design team strove to make Java:
* Simple, object oriented, and familiar
* Robust and secure
* Architecture neutral and portable
* High performance
* Interpreted, threaded, and dynamic
The reasons for omitting multiple inheritance from the Java language mostly stem from the "simple, object oriented, and familiar" goal. As a simple language, Java's creators wanted a language that most developers could grasp without extensive training. To that end, they worked to make the language as similar to C++ as possible (familiar) without carrying over C++'s unnecessary complexity (simple).
In the designers' opinion, multiple inheritance causes more problems and confusion than it solves. So they cut multiple inheritance from the language (just as they cut operator overloading). The designers' extensive C++ experience taught them that multiple inheritance just wasn't worth the headache.
Instead, Java's designers chose to allow multiple interface inheritance through the use of interfaces, an idea borrowed from Objective C's protocols. Multiple interface inheritance allows an object to inherit many different method signatures with the caveat that the inheriting object must implement those inherited methods.
Multiple interface inheritance still allows an object to inherit methods and to behave polymorphically on those methods. The inheriting object just doesn't get an implementation free ride. For an excellent discussion of interface inheritance, read Wm. Paul Rogers's "Reveal the Magic Behind Subtype Polymorphism"
简言之,任何一个Java Class,只能extend一个Class(一个父亲),但是它可以implement多个Interface。这样Java实现了OO得多态性,而又避免了C++ multiple inheritance造成的问题(Complex, hard to read, understand and maintain)
- 相关回复 上下关系8
😁Good for you! Highway 字0 2005-10-19 23:53:57
谢谢老大 风雨梧桐 字391 2005-10-19 21:35:20
昨天interview, 问我multiple inheritance wooxiao 字168 2005-10-18 17:46:00
😁这里有个解释,你先看看:
Thanks wooxiao 字0 2005-10-18 21:32:03
花!深入浅出。受教受教。 poorfat 字129 2005-10-17 23:35:41
班门弄斧一下 15 无斋主人 字2376 2005-10-18 12:40:15
😁说得很不错,鲜花一朵。不过呢, 1 Highway 字426 2005-10-18 13:36:17