My question is that can one interface extend another interface or it implements another interface.
interface A {
}
interface B extends A {
}
OR
interface B implements A {
}
My question is that can one interface extend another interface or it implements another interface.
interface A {
}
interface B extends A {
}
OR
interface B implements A {
}
Barring Java 8 extensions, an interface
doesn't implement anything.
So therefore extends
is the correct term.
An interface can only extend another interface, since an interface cannot contain any implementation.
interface ParentInterface{
void myMethod();
}
interface SubInterface extends ParentInterface{
void anotherMethod();
}
Yes, they can via the extends
keyword, as interfaces can't implement any methods, only declare their signatures.
interface A {
public void foo();
}
interface B extends A {
// You don't implement the methods from A here
public void bar();
}
class C implements B {
public void foo(){
}
public void bar(){
}
}
Any class that then implements B
must then implement the methods from A
as well.
Yes, an interface can extend another interface in Java.
// this interface extends from the Body interface:
public interface FourLegs extends Body
{
public void walkWithFourLegs( );
}
Well but we must think,
When would we want an interface to extend another interface
We know that any class that implements an interface must implement the method headings that are declared in that interface. And, if that interface extends from other interfaces, then the implementing class must also implement the methods in the interfaces that are being extended or derived from.
So, in the example above, if we have a class that implements the FourLegs interface, then that class must have definitions for any method headings in both the FourLegs interface and the Body interface.
Question part 2:Interface do not Implement another interface,Because
Implements denotes defining an implementation for the methods of an interface. However interfaces have no implementation so that's not possible.
In terms of Inheritance in case of "INTERFACE" they always extends they never implement anything.