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.
-
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– GhostCat2016-04-29 06:43:31 +00:00Commented Apr 29, 2016 at 6: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 moreDraken– Draken2016-04-29 06:45:06 +00:00Commented Apr 29, 2016 at 6:45
-
Yeah you are right @JägermeisterNizamuddin Shaikh– Nizamuddin Shaikh2016-04-29 06:46:06 +00:00Commented Apr 29, 2016 at 6: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– Draken2016-04-29 06:54:23 +00:00Commented Apr 29, 2016 at 6:54
-
Whoever marked this question as already answered could not understand the question clearly.nomadSK25– nomadSK252024-08-26 16:50:14 +00:00Commented Aug 26, 2024 at 16:50
Add a comment
|
1 Answer
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.
4 Comments
Nizamuddin Shaikh
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...
Peter Lawrey
@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.
Nizamuddin Shaikh
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. :)
Peter Lawrey
@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)