Skip to content
Advertisement

how to define a variable of a class type in java when the class has extends?

i have this class :

JavaScript

and the path interface:

JavaScript

where T , P and N are generic types. i want to define a variable FindPath in another class , and call it’s constructor , basically i want something like this :

JavaScript

( WNode,WNodePath are another classes i use in the generic types )

what am i doing wrong ? how to do this right ?

also another question :|| the WNodePath class is defined like so :

JavaScript

the path interface is Comparable , does that mean that also WNodePath is Comparable , which means that i can use it compareTo() function in a priority queue ?

Advertisement

Answer

When you declare a FindPath reference you don’t use extends, that’s used to provide bounds (limits) on the classes it is legal to use for that type parameter. (You can use extends when you have a wildcard type parameter, but that’s not relevant here, see https://docs.oracle.com/javase/tutorial/extra/generics/wildcards.html)

Just say:

JavaScript

Which compiles.

Yes, WNodePath implements Comparable, transitively via Path.

Advertisement