Java - Interface extending itself
Asked Answered
A

5

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!

Archaeo answered 13/2, 2013 at 19:26 Comment(2)
You should really accept one of the excellent answers.Onomatopoeia
This does an excellent job of explaining it: angelikalanger.com/GenericsFAQ/FAQSections/…Altagraciaaltaic
R
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.

Rawboned answered 13/2, 2013 at 19:29 Comment(7)
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?Altagraciaaltaic
@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.Rawboned
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.Altagraciaaltaic
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"?Cloudscape
@Cloudscape Close. I meant that the type parameter must be a subtype of PositionedVertex<V>.Rawboned
By subtype you mean any class that implements PositionedVertex (or extends a class that implements a PositionedVertex)?Cloudscape
@Cloudscape And with type parameters that fit the constraints. Also it could be the interface itself or an extending interface. Examples of subtypes.Rawboned
R
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.

Richly answered 13/2, 2013 at 19:28 Comment(1)
Thank you! Super quick answer!Archaeo
T
1

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

Teleutospore answered 13/2, 2013 at 19:28 Comment(0)
O
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.

Ovoid answered 13/2, 2013 at 19:29 Comment(0)
D
0

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

Dhruv answered 13/2, 2013 at 19:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.