Extending Java class with Scala Trait

I would like to define the ContextItem class as an extension of the java Predicate class with the Confidence sign.

Confidence is a simple trait that simply adds a confidence field to what it expands.

 trait Confidence{ def confidence:Double } 

I define my ContextItem class by simply stating:

 class ContextItem extends Predicate with Confidence{} 

But trying to compile this gives ...

 com/slug/berds/Berds.scala:11: error: overloaded method constructor Predicate with alternatives: (java.lang.String,<repeated...>[java.lang.String])com.Predicate <and> (java.lang.String,<repeated...>[com.Symbol])com.Predicate <and> (java.lang.String,java.util.ArrayList[com.Symbol])com.Predicate <and> (com.Predicate)com.Predicate <and> (com.Term)com.Predicate <and> (java.lang.String)com.Predicate cannot be applied to () class ContextItem(pred:Predicate) extends Predicate with Confidence{ ^ 

This seems like a trivial example, so what's wrong?

The predicate (which is not mine) looks like this:

 /** Representation of predicate logical form. */ public class Predicate extends Term implements Serializable { public Predicate(String n) { super(n); } public Predicate(Term t) { super(t); } public Predicate(Predicate p) { super((Term)p); } public Predicate(String n, ArrayList<Symbol> a) { super(n, a); } public Predicate(String n, Symbol... a) { super(n, a); } public Predicate(String n, String... a) { super(n, a); } @Override public Predicate copy() { return new Predicate(this); } } 

Neither the Predicate, nor any of its ancestors realize confidence.

+7
source share
1 answer

I think it lists all the Predicate constructors and tells you that you are not using any of them. By default, a constructor without parameters is used, which is not here. The call syntax, for example, a super constructor (String) , would be

 class ContextItem extends Predicate("something") with Confidence 

or

 class ContextItem(str: String) extends Predicate(str) with Confidence 

Also, at the moment, your def confidence is an abstract method, so it won’t compile until you define it. If you intended to add the property of the recorded confidence field, then this is exactly what you need:

 var confidence: Double = 0.0 
+6
source

All Articles