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

2 Answers 2

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]

Sign up to request clarification or add additional context in comments.

8 Comments

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?
Class A has 2 vars that have a List as Type and Java can't recognize those vars as fields of the class A.
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?
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.
Edited my post to answer you new questions.
|
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.