11

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!

2
  • 1
    You should really accept one of the excellent answers. Commented Mar 28, 2016 at 18:19
  • 1
    This does an excellent job of explaining it: angelikalanger.com/GenericsFAQ/FAQSections/… Commented Jun 1, 2022 at 4:57

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

7 Comments

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?
@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, 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.
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.
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 And with type parameters that fit the constraints. Also it could be the interface itself or an extending interface. Examples of subtypes.
|
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.

1 Comment

Thank you! Super quick answer!
1

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

Comments

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.