An interface can extend another interface in the same way that a class can extend another class. The extends keyword is used to extend an interface, and the child interface inherits the methods of the parent interface.
Java does not support "multiple inheritance" (a class can only inherit from one superclass). However, it can be achieved with interfaces, because the class can implement multiple interfaces. Note: To implement multiple interfaces, separate them with a comma (see example below).
Yes, you can do it. An interface can extend multiple interfaces, as shown here:
interface Maininterface extends inter1, inter2, inter3 {
// methods
}
A single class can also implement multiple interfaces. What if two interfaces have a method defining the same name and signature?
There is a tricky point:
interface A {
void test();
}
interface B {
void test();
}
class C implements A, B {
@Override
public void test() {
}
}
Then single implementation works for both :).
Read my complete post here:
http://codeinventions.blogspot.com/2014/07/can-interface-extend-multiple.html
An interface can extend multiple interfaces.
A class can implement multiple interfaces.
However, a class can only extend a single class.
Careful how you use the words extends
and implements
when talking about interface
and class
.
From the Oracle documentation page about multiple inheritance type,we can find the accurate answer here. Here we should first know the type of multiple inheritance in java:-
Java "doesn't support the multiple inheritance of state, but it support multiple inheritance of implementation with default methods since java 8 release and multiple inheritance of type with interfaces.
Then here the question arises for "diamond problem" and how Java deal with that:-
In case of multiple inheritance of implementation java compiler gives compilation error and asks the user to fix it by specifying the interface name. Example here:-
interface A {
void method();
}
interface B extends A {
@Override
default void method() {
System.out.println("B");
}
}
interface C extends A {
@Override
default void method() {
System.out.println("C");
}
}
interface D extends B, C {
}
So here we will get error as:- interface D inherits unrelated defaults for method() from types B and C interface D extends B, C
You can fix it like:-
interface D extends B, C {
@Override
default void method() {
B.super.method();
}
}
In Conclusion we can say that java doesn't support multiple inheritance of state but it does support multiple inheritance of implementation and multiple inheritance of type.
Can an interface extend multiple interfaces in java?
Answer is: Yes.
According to JLS
An interface may be declared to be a direct extension of one or more other interfaces, meaning that it implicitly specifies all the member types, abstract methods, and constants of the interfaces it extends, except for any member types and constants that it may hide.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With