I have come across this topic while reading interface in Java 8, there are scenarios where we define method in interface using default or static keyword, allowing the next child to either re-define the same method or implement it. Does that means multiple inheritance? There's one more issue that I found is that, return type must be co-variant type else compile issue, that means it still doesn't support multiple inheritance? Can we say that java supports multiple inheritance? Let me know more details in regard to this topic.
Asked
Active
Viewed 1.2k times
11
-
2@Draken I don't think that other question is really answering his question - as that question is not talking about Java 8 and default implementations within interfaces at all. – GhostCat Apr 29 '16 at 06:43
-
The confusion is in the usage of the word inheritance, inheritance technically only should be used for extending classes, not for interfaces. So we can't do multiple inheritance, but can do multiple implementation. I feel further reading on the subject would help a lot more – Draken Apr 29 '16 at 06:45
-
Yeah you are right @Jägermeister – Nizamuddin Shaikh Apr 29 '16 at 06:46
-
The other problem is that we're dealing with static methods in interfaces to make a faux inheritance, I would still avoid using that word as it brings more confusion to the table than it's worth. It's better to stick with more common terminology like overriding than saying it's inheritance. – Draken Apr 29 '16 at 06:54
1 Answers
10
Does that means multiple inheritance?
For interfaces, yes, but not classes. It is usually classes people think of as only classes can have fields and constructors. This is no different to Java 1.0
return type must be co-variant type else compile issue, that means it still doesn't support multiple inheritance?
The need for covariant returns type is not related to whether you have multiple inheritance or not.
Can we say that java supports multiple inheritance?
For interfaces, yes.

Peter Lawrey
- 525,659
- 79
- 751
- 1,130
-
If we are writing 2 different interface having same default method but with different return type then Java expects the return type to be co-variant type in the implementing class. Without co-variant type, Java throws compiler error. But to make it support multiple inheritance, return type may be different. We can use of static method to make our Java support multiple inheritance. Still there's a problem, we actually have to append Parent interface to use its static methods... – Nizamuddin Shaikh Apr 29 '16 at 08:29
-
1@ShaikhNizamuddin if you have two methods which return incompatible types, it may be confusing for the developer to give the methods the same name. I would suggest giving different names so it is clear for the developer using it what result they should expect to get. – Peter Lawrey Apr 29 '16 at 09:29
-
Yeah its good to use different names for the methods having different return type. Maybe in the upcoming Java versions(bit of assumption, being optimistic) we can see this co-variant type to be non-ambiguous. :) – Nizamuddin Shaikh Apr 29 '16 at 10:03
-
1@ShaikhNizamuddin Type inference is new in Java, however it could use type inference to determine which overloaded method should be called. NOTE: The JVM includes the return type in the method signature so it supports methods with the same name with different return types (and there was a bug in the Java 6 compiler which allowed you to use it) – Peter Lawrey Apr 29 '16 at 10:11