Integrating Java with Scala: Extending from Scala classes

Go To StackoverFlow.com

0

I have created a project that uses scala that has some abstract classes definitions such as:

abstract class A(n: String) {
    require(n!= null)
    val name = n

    var inputs: List[Input[_]]
    var outputs: List[Output[_]]
}

abstract class B(n: String) extends A(n){  
   def act
   def isValid : Boolean
   def getCs : List[C[_]]
}

abstract class C[Type]{
   var value: Type 
}

Now I want to use that project in Java. I have imported scala lib in a new project and added scala project in the build path. I am using eclipse.

However, when I try to do this:

public class CoolClass extends B {  
    public CoolClass(){ }   
}

EDIT:

I have some problems using this in Java that raised me some doubts. Let me enumerate them:

  1. Can I use Lists from Scala or that gives errors in Java?
  2. Why vars from A aren't recognized in classes that extend B in Java?
  3. How can I declare getCs so it provides an interface with List<C> instead of List<C<?>> ?
2012-04-05 22:31
by Tiago Almeida


7

I'm just assuming, that you implemented all the methods you define in you class B also in your java class CoolClass. What is missing here is the call to B's constructor, as the error message says.

class CoolClass extends B {
  public CoolClass() {
    super("");
  }
  // methods implementations
}

There is no default constructor defined on your class B, so you have to explicitly call the one that is defined, which is B(n: String).

edit:

1: You can use anything you want on both sides. Though the method names may be different in some special cases like anything that includes special chars in scala.

2: They are recognized, though they are not fields, but methods in the java code. You can access them with this.inputs() and this.outputs()

3: Try def getCs[C]: List[C]

2012-04-05 22:47
by drexin
The constructer solved some problems but I keep getting problems and many of them don't make sense. I keep with one doubt: Can I use Lists (from Scala) ? Or Java doesn't like that - Tiago Almeida 2012-04-06 10:25
Class A has 2 vars that have a List as Type and Java can't recognize those vars as fields of the class A - Tiago Almeida 2012-04-06 10:27
Is the code you posted the code you have problems with? Your def getCs : List[C[_]] will not compile, because C is not known. You are talking of 2 fields in the A class, could you please edit your original post accordingly - drexin 2012-04-06 10:33
Sorry for my incomplete question. I thought that most of the problems would be solved with the first problems I posted here. Added one more problem at my question to prevent repeated information on StackOverflow - Tiago Almeida 2012-04-06 10:42
Edited my post to answer you new questions - drexin 2012-04-06 10:56
1 - I was talking about Scala List - Tiago Almeida 2012-04-06 12:23
(Sorry forgot that enter adds the comment)

2 - I couldn't acess the fields of a class with this.field(). Maybe I should declare a method for each field?

3 - Almost worked. However it returns a <Object> instead of <C>

I belive scala doesn't integrate that well with Java. Maybe I do the basic structure with Java and then I add some layers to my work with scala. Probly with classes that aren't going to be extended in Java and don't use Generic this works better - Tiago Almeida 2012-04-06 12:26

Yes, so was I. I was just saying, that you can use any scala class in java and the other way around, this includes scala.collection.immutable.List - drexin 2012-04-06 12:27


1

Your B class is essentially as you describe:

public abstract class B extends A {  
  public B(String n) {
    super(n);
  }

  public abstract void act()
  public abstract boolean isValid();
  public abstract List<C<?>> getCs();
}

Your CoolClass is missing the explicit super call in the constructor:

public CoolClass extends B {
  public CoolClass() { // override ctor with no string parameter
    super("defaultStringValue");
  }

  public CoolClass(String n) { // override ctor with no string parameter
    super(n);
  }
  …

As you can see Scala makes it so trivial to declare and call constructors we forget how much boilerplate they add in Java.

2012-04-06 02:52
by Jed Wesley-Smith
Ads