I've been using this site for about 6 months now, and its time to ask my first question, because I cant find the answer to this, atleast not an answer that I can understand!
In this bit of code, why is this interface extending itself?
public interface PositionedVertex<V extends PositionedVertex<V>> {
/**
* @return Position for node data.
*/
public Point getPosition();
}
Wouldnt this code do the same?:
public interface PositionedVertex<V> {
/**
* @return Position for node data.
*/
public Point getPosition();
}
Thanks in advance!
No, a class cannot extend itself in java.
Extending InterfacesAn 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 allow extending multiple classes.
In this example, an object of type Mammal has both the instance variable weight and the method eat() . They are inherited from Animal . A class can extend only one other class. To use the proper terminology, Java allows single inheritance of class implementation.
The interface isn't extending itself. The <V extends PositionedVertex<V>>
is a bound on the generic type that is associated with your interface. It just means that the generic type parameter for any class that implements this interface must itself be a PositionedVertex
.
In the first case, you have bounded your generic type parameters to be subtype of the interface
itself, whereas, in the second case, you can have any type as generic type parameter. So, they are potentially different declarations.
For example, you can define a reference like:
PositionedVertex<String>
for the 2nd interface type
, but not for the 1st one
.
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