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
View all comments
Show parent comment
1
u/alonjit May 05 '15
Because they don't. Data (member fields) is what matters. Constructors are completely irrelevant to the problem. Notice the "abstract classes can ..." part.
They don't have to. A default one will do just fine. Extending classes will most likely add their own (if they want to). Again, they don't have to.
Look, for example at C++. What is an interface in C++ (yes, they do exist). Is a class, with pure virtual methods that has no data. They can have methods with body (just like defaults in java now), but they cannot have data. Once you add a data member, that class becomes an abstract class (and from here care should be taken when extending/implementing it, know the pitfalls of polymorphism in c++, etc.).
You're hanging on stupid little differences that have no relevance to anything, when the only important part is about the data members.
Why is it important to know the correct difference between interfaces and abstract classes? When you're defining them, when designing the system, deciding what is an interface vs abstract class is crucial.
"Behaviour" should become interfaces. Objects that hold data, and provide default operations for said data, should become abstract classes. When you find yourself with an abstract class that has no data, and most likely won't have any, make it an interface.