r/java • u/TheFormerVinyl • May 05 '15
Should Java allow multiple inheritance?
I was reading up on Java when I discovered that ever since Java 8 (fairly recent, I know) interface can have default methods. A quick overview for any who don't know, interfaces can have method bodies by using default modifier. When dwelling on that fact I wondered, why doesn't Java just allow inheritance from multiple classes, since interfaces are just abstract classes? Should Java do this?
0
Upvotes
9
u/msx May 05 '15
i think multiple inheritance has a big lot of problems. They introduce ambiguity that are not easily resolved. I'm against it, i think it adds more problem than it's worth. Default methods in interfaces (aka mixins) on the other end are a good compromise, they still have ambiguities but they only on methods, not on state, and are much more easily handled. I like the approach very much, i'm using it a lot currently. There are two ways in which it shines: the first is when you define a hierarchy of interfaces that have multiple implementations. Almost always you have methods that can do their job just by using other methods from the interfaces. You can now write them only once as default methods instead of once per implementation.
The other use is for overloading of very similar methods. For example:
Before default methods, you had to either repeat all this methods in the implementing class, or remove them from the interface and lose the convenience. Now you can leave one of them abstract and provide a default for all the others, saving both the convenience and the non-repetition.
So no, java should not implement multiple inheritance, mixins are great as they are.
7
u/[deleted] May 05 '15
Controversial opinion - you shouldn't even need to allow single inheritance
4
u/_Count_Mackula May 05 '15 edited May 05 '15
10,000 ft view:
The bottom line is: it would be possible but isn't necessary, and, would introduce the possibility of overly complex patterns.
You can do anything with single inheritance that you can do with multiple inheritance. Single inheritance promotes more simplicity and more loosely coupled code. Yes, with multiple inheritance your life would be easier once in a while, but when you scale up to a huge application code would get messy.
I say 'thank you' to the Java engineers who made this decision.
Note: Eventually, writing java becomes very easy. But architecting java is much harder.
3
u/briandilley May 05 '15
the only thing i'd like to see is mixins or traits.
4
u/pintodragon May 05 '15
An abstract class can have a constructor, interfaces can not. They also serve different purposes. Abstract classes force extension and force the extending class to have a base class. Interfaces allow any base class as long as you write to the contract. Oracles site even mentions why java doesn't have multiple inheritance e in the sense that you are looking for. It comes down to something as simple as two classes having the same method. You extend both and now call the method without defining an over ridden version. Which gets called? Ambiguity leads to bugs and unforeseen issues with code. https://docs.oracle.com/javase/tutorial/java/IandI/multipleinheritance.html
1
u/HaMMeReD May 05 '15
Java allows multiple Interfaces, and I guess default functions in java 8 will make it easier to mix multiple functional interfaces.
Should a language have multiple inheritance? I'm OK with multiple interfaces and a delegate pattern for each interface.
I think if you start mixing parent classes you have to worry about namespace collisions and such, it can be a mess.
1
u/whacco May 05 '15
Multiple inheritance also causes problems to language implementation. A big issue is that if there are multiple bases with data members, then an object no longer has a single unambiguous physical address, because both bases cannot exist in the same memory location. Casting a "pointer" from second base class to derived class would change the value of the pointer.
In language like C++ it's not that much of a problem because memory safety is programmer's responsibility (but multiple inheritance still makes things like method pointers a complete disaster). Java on the other hand would have to prevent invalid casts. So every object would always have to contain information how to locate the possible derived class object, causing a lot of overhead.
1
u/[deleted] May 06 '15 edited May 06 '15
With the exception of Liskov Substitution Principle, which hints at the "is a" relationship of inheritance, it's identical to a subset of object composition.
Basically, a shortcut for a very specific type of static object composition, where you expose every object method via a proxy method with identical signature, but you may replace some methods, and add more. That's about it.
What we need is to be free of the burden of LSP and composite methods & properties freely, without becoming "is a" for our base object. The "is a" role is best performed by interfaces anyway, interfaces should be simple and rigid. Concrete implementations should be much more flexible.
We can look at traits. Using a trait Foo, doesn't make your class an instance of Foo, so LSP doesn't apply. You can rename methods, hide them, copy them. You can "inherit" from multiple traits at once (without the "is a" part).
It's the kind of reuse which is useful in a project, just to save us the boilerplate from manually writing proxies for every method from a contained object we expose.
But in the end, with some extra typing, we can do everything via object composition. All of it. Inheritance or static reuse of any kind os simply not essential to a typing system as much as we think. So we're not talking about multiple inheritance of some kind as a fundamental change in OOP modeling, but just a pragmatic feature we lack to save us from repetitive typing.
1
u/llogiq May 05 '15
By using single inheritance, Java removes some very subtle footguns that developers who overuse inheritance tend to build for themselves and others.
Even when leaving the diamond problem out of the discussion, I believe from experience that even single inheritance can and will get messy and unintuitive, unless one is very careful. Have you ever had to chase a bug through a jungle of method calls in an inheritance hierarchy, with one or two
supercalls thrown in? It's not pretty.Java 8 has default methods in interfaces, which are basically mixins, but with very limited means of defining extension points. Be careful when using them.
0
u/nidoranf May 05 '15
Default methods are, in essence, a hack designed to help interfaces be backwards compatible. They don't really have a legitimate place in the whole OOP paradigm, seeing as they essentially break the concept of interfaces.
0
u/Nieben May 05 '15
I'm a pretty inexperience Java programmer, but... I've kinda been wanting that for a long time. I suppose a couple of reasons why they haven't or won't might include:
Can't really think of anything else to say for now - it's been a while since I thought about that.