Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extending Java Class with Scala Trait

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

Confidence is a simple trait which simply adds a confidence field to whatever it extends.

trait Confidence{
  def confidence:Double
}

I am defining my ContextItem class by simply stating:

class ContextItem extends Predicate with Confidence{}

But attempting to compile this yields...

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 going wrong?

Predicate (which is not mine) looks like:

/** 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 Predicate nor any of its ancestors implements confidence.

like image 320
williamstome Avatar asked Apr 17 '13 20:04

williamstome


People also ask

Can a class extend a trait Scala?

Classes and objects can extend traits, but traits cannot be instantiated and therefore have no parameters.

What is Scala trait in Java?

In scala, trait is a collection of abstract and non-abstract methods. You can create trait that can have all abstract methods or some abstract and some non-abstract methods. A variable that is declared either by using val or var keyword in a trait get internally implemented in the class that implements the trait.

Is trait in Scala same as interface in Java?

Traits in Scala have a lot of similarities with interfaces in Java, but a trait is more powerful than an interface because it allows developers to implement members within it. The trait is a combination of abstract and non-abstract methods. Trait can not be instantiated, thus it has no parameters.


1 Answers

I think it is listing all the constructors of Predicate, and informing you that you're not using any of them. The default is to use a parameterless constructor, which doesn't exist here. The syntax to call, for example, the (String) super-constructor, 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 won't compile until you give it a definition. If you intended the trait to add a writable confidence field then this is what you want instead:

var confidence: Double = 0.0
like image 173
Nick Avatar answered Sep 16 '22 18:09

Nick