Reputation: 8905
I have multiple constructor in a Java class.
public class A{
public A(){...}
public A(String param){...}
public A(String param, Object value}
}
Now I want to create a Scala class that inherit from that class
class B extends A{
super("abc")
}
But this syntax is invalid. Scala is complaining that '.' expected but '(' found.
What is the valid way to do this?
Upvotes: 20
Views: 13984
Reputation: 54574
As Moritz says, you have to provide the constructor args directly in the class definition. Additionally you can use secondary constructors that way:
class B(a:String, b:String) extends A(a,b) {
def this(a:String) = this(a, "some default")
def this(num:Int) = this(num.toString)
}
But you must refer to this
, super
is not possible.
Upvotes: 31
Reputation: 14212
You have to append the constructor arguments to the class definition like this:
class B extends A("abc")
Upvotes: 30