`
rensanning
  • 浏览: 3513926 次
  • 性别: Icon_minigender_1
  • 来自: 大连
博客专栏
Efef1dba-f7dd-3931-8a61-8e1c76c3e39f
使用Titanium Mo...
浏览量:37478
Bbab2146-6e1d-3c50-acd6-c8bae29e307d
Cordova 3.x入门...
浏览量:604304
C08766e7-8a33-3f9b-9155-654af05c3484
常用Java开源Libra...
浏览量:678049
77063fb3-0ee7-3bfa-9c72-2a0234ebf83e
搭建 CentOS 6 服...
浏览量:87250
E40e5e76-1f3b-398e-b6a6-dc9cfbb38156
Spring Boot 入...
浏览量:399811
Abe39461-b089-344f-99fa-cdfbddea0e18
基于Spring Secu...
浏览量:69066
66a41a70-fdf0-3dc9-aa31-19b7e8b24672
MQTT入门
浏览量:90468
社区版块
存档分类
最新评论

【转】Why Multiple Inheritance is Not Supported in Java

    博客分类:
  • Java
 
阅读更多
Why Multiple Inheritance is Not Supported in Java

In an white paper titled “Java: an Overview” by James Gosling in February 1995 gives an idea on why multiple inheritance is not supported in Java.
引用
JAVA omits many rarely used, poorly understood, confusing features of C++ that in our experience bring more grief than benefit. This primarily consists of operator overloading (although it does have method overloading), multiple inheritance, and extensive automatic coercions.

Who better than Dr. James Gosling is qualified to make a comment on this. This paragraph gives us an overview and he touches this topic of not supporting multiple-inheritance.

Java does not support multiple inheritance
First lets nail this point. This itself is a point of discussion, whether java supports multiple inheritance or not. Some say, it supports using interface. No. There is no support for multiple inheritance in java. If you do not believe my words, read the above paragraph again and those are words of the father of Java.

This story of supporting multiple inheritance using interface is what we developers cooked up. Interface gives flexibility than concrete classes and we have option to implement multiple interface using single class. This is by agreement we are adhering to two blueprints to create a class.

This is trying to get closer to multiple inheritance. What we do is implement multiple interface, here we are not extending (inheriting) anything. The implementing class is the one that is going to add the properties and behavior. It is not getting the implementation free from the parent classes. I would simply say, there is no support for multiple inheritance in java.

Multiple Inheritance
Multiple inheritance is where we inherit the properties and behavior of multiple classes to a single class. C++, Common Lisp, are some popular languages that support multiple inheritance.


Why Java does not support multiple inheritance?

Now we are sure that there is no support for multiple inheritance in java. But why? This is a design decision taken by the creators of java. The keyword is simplicity and rare use.

Simplicity

I want to share the definition for java given by James Gosling.
引用
JAVA: A simple, object oriented, distributed, interpreted, robust, secure, architecture neutral, portable, high performance, multithreaded, dynamic language.

Look at the beauty of this definition for java. This should be the definition for a modern software language. What is the first characteristic in the language definition? It is simple.

In order to enforce simplicity should be the main reason for omitting multiple inheritance. For instance, we can consider diamond problem of multiple inheritance.


We have two classes B and C inheriting from A. Assume that B and C are overriding an inherited method and they provide their own implementation. Now D inherits from both B and C doing multiple inheritance. D should inherit that overridden method, which overridden method will be used? Will it be from B or C? Here we have an ambiguity.

In C++ there is a possibility to get into this trap though it provides alternates to solve this. In java this can never occur as there is no multiple inheritance. Here even if two interfaces are going to have same method, the implementing class will have only one method and that too will be done by the implementer. Dynamic loading of classes makes the implementation of multiple inheritance difficult.

Rarely Used

We have been using java for long now. How many times have we faced a situation where we are stranded and facing the wall because of the lack of support for multiple inheritance in java? With my personal experience I don’t remember even once. Since it is rarely required, multiple inheritance can be safely omitted considering the complexity it has for implementation. It is not worth the hassle and the path of simplicity is chosen.

Even if it is required it can be substituted with alternate design. So it is possible to live without multiple inheritance without any issues and that is also one reason.

My opinion on this is, omitting support for multiple inheritance in java is not a flaw and it is good for the implementers.
  • 大小: 108.3 KB
  • 大小: 9.7 KB
分享到:
评论

相关推荐

    Type.Inheritance.and.Relational.Theory

    Type inheritance is that phenomenon according to which we can say, for example, that every square is also a rectangle, and so properties that apply to rectangles in general apply to squares in ...

    《Programming in Java Second Edition》高清完整英文PDF版

    This second edition is not just about the enhancements introduced in Java 7; practically every chapter has been revisited to refine the text as much as possible with new example codes and greater ...

    Interactive.Object.Oriented.Programming.in.Java

    Discover object oriented programming with Java in this unique tutorial. This book uses Java and Eclipse to write and generate output for examples in topics such as classes, interfaces, overloading, ...

    C# to Java 代码转换工具

    Code snippet and file conversion: Our snippet conversion accuracy is outstanding and does not require you to insert entire methods or classes. Heuristics are used to convert code fragments wit h ...

    Inheritance

    PPT格式的文档 关于uml的课件,关于java的继承问题

    Killer Game Programming In Java

    games programming, for entertainment or as the first step in becoming a games programming professional. You want to write a game that uses the latest Java technology, not an applet showing a penguin ...

    JavaScript Object Programming(Apress,2015)

    JavaScript’s object programming (not inheritance) is what separates it from classical OOP languages like C++ and Java. Most important, basing inheritance on JavaScript’s prototypal chain is possible...

    Java in Two Semesters Featuring JavaFX, 4th Edition

    Offering a gentle introduction to the field, assuming no prior knowledge of the subject, Java in Two Semesters is the ideal companion to undergraduate modules in software development or programming.

    JAVA白皮书(英文版)

    2.2.5No More Multiple Inheritance 2.2.6No More Goto Statements 2.2.7No More Operator Overloading 2.2.8No More Automatic Coercions 2.2.9No More Pointers 2.3Summary 3. Java is Object ...

    Java.Crash.Course.1541019245.epub

    "Java Crash Course" contains a multitude of tips and tricks, examples and exercises you can do to grow your Java programming skills to unprecedented levels. We chose the content of this book carefully...

    Java 9 with JShell

    The release of Java 9 has brought many subtle and not-so-subtle changes to the way in which Java programmers approach their code. The most important ones are definitely the availability of a REPL, ...

    JavaScript.Object.Programming.148421

    JavaScript's object programming (not inheritance) is what separates it from classical OOP languages like C++ and Java. Most important, basing inheritance on JavaScript's prototypal chain is possible, ...

    面向对象编程:C++与Java比较教程 英文版

    Chapter 16 - Multiple Inheritance in C++ Chapter 17 - OO for Graphical User Interfaces, A Tour of Three Toolkits Chapter 18 - Multithreaded Object-Oriented Programming Chapter 19 - Network Programming...

    Java_programming_code_inheritance_Thread.rar_inheritance

    Java编程继承Thread实现线程代码Java programming code inheritance Thread to Thread

    java_inheritance_and_polymorphism:Java中的继承和多态性练习

    java_inheritance_and_polymorphism Java中的继承和多态性练习

    Java.to.Python 高清完整epub版

    If Java is the heavy metal of computer programming, then Python is the jazz that opens doors of freedom in software development. Both Java and Python are object-oriented programming languages. Both ...

    JAVA基础之继承(inheritance)详解

    继承(inheritance)是Java OOP中一个非常重要的概念。这篇文章主要介绍了JAVA基础之继承(inheritance),需要的朋友可以参考下

    Google C++ Style Guide(Google C++编程规范)高清PDF

    However, implementation code properly belongs in .cc files, and we do not like to have much actual code in .h files unless there is a readability or performance advantage. If an inline function ...

Global site tag (gtag.js) - Google Analytics