Java is not allowing inheritance from multiple classes (still it allows inheritance from multiple interfaces.), I know it is very much inline with classic diamond problem. But my questions is why java is not allowing multiple inheritance like C++ when there is no ambiguity (and hence no chances of diamond problem) while inheriting from multiple base class ?
-
3If you want mixins(=the good parts of MI) in a jvm language, have a look at scala.Kim Stebel– Kim Stebel2009-08-11 20:48:45 +00:00Commented Aug 11, 2009 at 20:48
-
In my experience, multiple-inheritence can make for very brittle code. Using interfaces is much healthier. If you want to re-use implementation logic, use delegation. Modern IDE's like Eclipse make it easy to delegate work from one class to another.JohnnySoftware– JohnnySoftware2010-01-02 01:00:13 +00:00Commented Jan 2, 2010 at 1:00
-
For anyone looking for a detailed article on 'Mixins', here is one. As the article states, only 1 interface is needed if the classes do not need services from one another. If that is the case, just read the 2nd section.MattC– MattC2013-03-22 21:45:06 +00:00Commented Mar 22, 2013 at 21:45
-
1@JohnnySoftware Multiple inheritance makes code brittle but multiple inheritance of interface classes makes good code?curiousguy– curiousguy2015-11-02 03:48:43 +00:00Commented Nov 2, 2015 at 3:48
9 Answers
It was a design decision of Java. You'll never get it, so don't worry too much about it. Although MI might help you make Mixins, that's the only good MI will ever do you.
Comments
I have read that most programmers don't use multiple inheritance in a proper way. "Just go ahead and inherit from a class just to reuse code" is not the best practice in case of multiple inheritance.
Many programmers do not know when to use simple inheritance in most cases. Multiple inheritance must be used with caution and only if you know what you are doing if you want to have a good design.
I don't think that the lack of multiple inheritance in java (as in c++) will put restrictions in your code / application design / problem domain mapping into classes.
Comments
Simplicity. To quote Tom Sintes,
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.
Comments
if java support multiple inheritance then it may effect other features of java
consider super() method which is used to call super class constructor.if program has multiple super class(because of multiple inheritance) then compiler will get confused about which super class constructor should be called and throw an error
Comments
One simple answer is that all classes in Java derive from java.lang.Object IIRC. So, you would always have a diamond problem... :-D
5 Comments
It is true that Java did not use to support multiple inheritance of implementation (just of type i.e. interface). That was a design decision.
However, since Java 8, it supports multiple inheritance using default methods. See http://docs.oracle.com/javase/tutorial/java/IandI/multipleinheritance.html:
Multiple inheritance of implementation is the ability to inherit method definitions from multiple classes. Problems arise with this type of multiple inheritance, such as name conflicts and ambiguity. ... Default methods introduce one form of multiple inheritance of implementation.
1 Comment
package org.example;
public class Main {
public static void main(String[] args) {
Two_D circle = new Circle(5);
System.out.println("Area of circle: " + circle.Area());
Two_D square = new Square(4);
System.out.println("Area of square: " + square.Area());
Two_D triangle = new Triangle(3, 4, 5);
System.out.println("Area of triangle: " + triangle.Area());
Three_D sphere = new Sphere(3);
System.out.println("Surface area of sphere: " + sphere.SurfaceArea());
System.out.println("Volume of sphere: " + sphere.Volume());
Three_D cube = new Cube(4);
System.out.println("Surface area of cube: " + cube.SurfaceArea());
System.out.println("Volume of cube: " + cube.Volume());
}
}
class Shape {
}
class Two_D extends Shape {
double Area() {
return 0;
}
}
class Three_D extends Shape {
double SurfaceArea() {
return 0;
}
double Volume() {
return 0;
}
}
class Circle extends Two_D {
double radius;
Circle(double radius) {
this.radius = radius;
}
@Override
double Area() {
return Math.PI * radius * radius;
}
}
class Square extends Two_D {
double side;
Square(double side) {
this.side = side;
}
@Override
double Area() {
return side * side;
}
}
class Triangle extends Two_D {
double a, b, c;
Triangle(double a, double b, double c) {
this.a = a;
this.b = b;
this.c = c;
}
@Override
double Area() {
double s = (a + b + c) / 2;
return Math.sqrt(s * (s - a) * (s - b) * (s - c));
}
}
class Sphere extends Three_D {
double radius;
Sphere(double radius) {
this.radius = radius;
}
@Override
double SurfaceArea() {
return 4 * Math.PI * radius * radius;
}
@Override
double Volume() {
return (4 / 3) * Math.PI * Math.pow(radius, 3);
}
}
class Cube extends Three_D {
double side;
Cube(double side) {
this.side = side;
}
@Override
double SurfaceArea() {
return 6 * side * side;
}
@Override
double Volume() {
return side * side * side;
}
}
2 Comments
The diamond problem arises when multiple parent classes define their own implementations of something and the child class of these two has to deal with the ambiguity of which implementation to use. So what if all classes in Java derive from Object, that's a single parent class. "Single parent, multiple derived classes" is not the same as "Multiple parents, single derived class"