Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - Interface extending itself

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!

like image 471
user2069658 Avatar asked Feb 13 '13 19:02

user2069658


People also ask

Can an interface extend itself in Java?

No, a class cannot extend itself in java.

Why interface extends interface?

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.

Can class extend more than one class?

Java does not allow extending multiple classes.

How many classes can a class extend in Java?

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.


2 Answers

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.

like image 122
rgettman Avatar answered Nov 09 '22 23:11

rgettman


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.

like image 45
Rohit Jain Avatar answered Nov 10 '22 00:11

Rohit Jain