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

【转】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
分享到:
评论

相关推荐

    《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 ...

    java英文笔试

    While Java does not directly support multiple inheritance due to potential complications and ambiguities, you can achieve similar functionality using interfaces, which allow for multiple inheritance ...

    Inheritance

    《继承在Java编程中的应用与理解》 继承是面向对象编程中的核心概念之一,它允许开发者从已有的类(父类或超类)派生出新的类(子类或派生类),以此来实现代码的复用和扩展。在Java编程语言中,继承通过关键字`...

    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 ...

    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 - A Beginner’s Guide - Sixth Edition - Herbert Schildt

    Implementing interfaces is a way to achieve multiple inheritance in Java. #### Exception Handling Exception handling is a mechanism for dealing with errors and unexpected situations in a program. ...

    Thinking in Java 课文中的代码

    《Thinking in Java》是Bruce Eckel的经典编程教材,它深入浅出地介绍了Java语言的核心概念和技术。这本书通过实例代码来讲解理论,使读者能够更好地理解和掌握Java编程。在这个压缩包中,我们很可能会找到与书中的...

    inheritance-in-java:Java继承的完美实践代码

    在这个"inheritance-in-java:Java继承的完美实践代码"中,我们将深入探讨Java继承的概念、优势以及如何在实践中运用。 1. 继承的概念 在Java中,继承是通过使用`extends`关键字来实现的。一个子类可以继承一个或多...

    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, ...

    ObjCRuntimeGuide

    - **Forwarding and Multiple Inheritance:** In Objective-C, multiple inheritance is not directly supported. However, the document discusses how message forwarding can be used in scenarios involving ...

    Thinking in Java 4th Edition with Annotated Solution Guide编程思想+课后习题源码

    《Thinking in Java 4th Edition with Annotated Solution Guide》是一本经典的Java编程教材,由Bruce Eckel撰写。这本书深入浅出地介绍了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 ...

    Thinking in java 4th eidtion

    ### 《Thinking in Java》第四版关键知识点综述 #### 一、书籍概述与评价 《Thinking in Java》第四版是一本备受推崇的经典Java编程教材,由Bruce Eckel撰写,适用于初学者及进阶读者。本书自出版以来,便成为众多...

Global site tag (gtag.js) - Google Analytics