AKR
AKR

Reputation: 531

Scala: Use Java Constructor with Subclasses in Scala

I want to use a constructor, that is written in Java, in Scala.

The constructor is declared in that way:

public <SUBCLASS extends Node> NodeDock(Parent<? super Node> parent, Class<SUBCLASS> cls, LookupCriteria<SUBCLASS>[] criteria) {
   this(parent, cls, 0, criteria);
}

So if i want to use it:

val task = new NodeDock(scene.asParent(), classOf[FXTaskStackElement].asInstanceOf[Class[_]], new LookupCriteria[FXTaskStackElement]() {...}

Scala is giving me always an error that he cannot find the appropriate constrcutor with these parameters. So how can i get the SUBCLASS of FXTaskStackElement for the LookupCriteria?

Edit: In Java i would call this constrcutor like that, which works fine:

task = new NodeDock(scene.asParent(), FXTaskStackElement.class, new LookupCriteria<FXTaskStackElement>() {...})

Upvotes: 1

Views: 135

Answers (1)

Alexey Romanov
Alexey Romanov

Reputation: 170733

Why are you using classOf[FXTaskStackElement].asInstanceOf[Class[_]] instead of just classOf[FXTaskStackElement]? Since your second argument is a Class[_], there is no suitable SUBCLASS.

Upvotes: 1

Related Questions