10

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!

Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
user2069658
  • 101
  • 4

5 Answers5

13

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.

rgettman
  • 176,041
  • 30
  • 275
  • 357
  • this confuses me, why do we need generic type at all in this case then? Could this not be achieved by not using generic at all in this example? – Mr Matrix May 31 '22 at 23:27
  • @MrMatrix In the explicit code from the question, there is no purpose. It's assumed that other code in the class but not in the question needs the `this` type according to the [Curiously Recurring Template Pattern](https://en.wikipedia.org/wiki/Curiously_recurring_template_pattern), where a family of classes needs to refer to their own specific types and not the superclass/interface. For objects with methods that need to return `this`, e.g. the Build Pattern, fluent programming, and polymorphic chaining, this is quite a useful self-reference. – rgettman May 31 '22 at 23:56
  • Thanks for explaining this, I learnt something new. Can you explain how this is different from parent(superclass/interface)-child relationship? I am unable to get my mind around this subtlety. – Mr Matrix Jun 01 '22 at 00:22
  • You mention "the generic type parameter for any class that implements this interface must itself be a PositionedVertex". This doesn't make sense. Do you mean "the generic type parameter for any class that implements this interface must itself implement a PositionedVertex"? – Mts Nov 15 '22 at 18:12
  • @Mts Close. I meant that the type parameter must be a subtype of `PositionedVertex`. – rgettman Nov 15 '22 at 18:18
  • By subtype you mean any class that implements PositionedVertex (or extends a class that implements a PositionedVertex)? – Mts Nov 15 '22 at 18:21
  • 1
    @Mts And with type parameters that fit the constraints. Also it could be the interface itself or an extending interface. [Examples of subtypes](https://docs.oracle.com/javase/tutorial/java/generics/inheritance.html). – rgettman Nov 15 '22 at 18:27
4

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.

jahroy
  • 22,322
  • 9
  • 59
  • 108
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
1

It is not extending itself. It is specifying that its generic parameter V must represent a type that extends or implements itself.

Patricia Shanahan
  • 25,849
  • 4
  • 38
  • 75
0

There is no telling why without showing some more code. Or is this all? What it is actually telling you is that there is some type V used somewhere in this interface (as a parameter to a function, or as a return type) that is of the same type as the interface itself.

Rens Verhage
  • 5,688
  • 4
  • 33
  • 51
0

This is extension for generic. More info about generics: http://docs.oracle.com/javase/tutorial/extra/generics/intro.html

meduoliai
  • 103
  • 2
  • 9