0
Can we Achieve multiple inheritance using interface??If it's yes give one example else give reason?
5 Réponses
+ 6
In java you can not inherit more than one class because it's make harder to maintain the codes.But java provides interface , actually it limited to 65,535 due to jvm limitations.You can not implement more than 65,535 interfaces.Find more about interfaces here
https://www.sololearn.com/discuss/1923650/?ref=app
https://www.sololearn.com/discuss/43857/?ref=app
https://www.sololearn.com/discuss/148094/?ref=app
https://www.sololearn.com/discuss/837263/?ref=app
https://www.sololearn.com/discuss/1645457/?ref=app
https://www.sololearn.com/discuss/1669267/?ref=app
+ 4
Interfaces can also have a default method which contains the implementation. This is similar to an abstract class having a concrete method. That way you can actually do multiple inheritance via interfaces (this is also called "Mixin").
Here is a detail example
http://hannesdorfmann.com/android/java-mixins
+ 4
See if this makes sense-
https://code.sololearn.com/cLr6kiVuZc0A/?ref=app
+ 1
with interfaces you can achieve sort of multiple. implementing an interface doesn't give the 'child'-class the method it just forces it to have that method
hope this helps you 😁
+ 1
multiple inheritance is possible if there is not conflict with same method name
(or if you reimplement conflict method)
interface A { default String m(){return "A";} }
interface B { default String m(){return "B";} }
public class Prg implements A,B { //error
public static void main(String[] args) {
var p = new Prg();
System.out.println( p.m() );
}
}