Question summary - How do I convert this to a Scala class?
Issue - Multiple constructors calling different super constructors
Java class -
public class ClassConstExample extends BaseClassExample {
private String xyzProp;
private string inType = "def";
private String outType = "def";
private String flagSpecial = "none";
public ClassConstExample(final String file, final String header, final String inType,
final String outType, final String flag) {
super(file);
init(header, inType, outType, flag);
}
public ClassConstExample(final String file, final String header, final String inType,
final String outType, final String flag, final String mode) {
super(file, mode);
init(header, inType, outType, flag);
}
public ClassConstExample(final String file, final String header, final String flag){
super(file);
//some logic here that's irrelevant to this
...
this.xyxProp = getXYZ(header);
this.flagSpecial = getFlagSpecial(flag);
}
...
}
I have been trying to convert these constructors for this class to scala for about a day and I cannot make any headway on how to deal with the following issue - (Multiple constructors calling different base class constructors in Scala). Would anyone mind helping me on a way to approach converting this class? I have read some places said it's not possible to do this with standard super
calling in Scala, then how do I accomplish this?
With super(parameter list) , the superclass constructor with a matching parameter list is called. Note: If a constructor does not explicitly invoke a superclass constructor, the Java compiler automatically inserts a call to the no-argument constructor of the superclass.
To explicitly call the superclass constructor from the subclass constructor, we use super() . It's a special form of the super keyword. super() can be used only inside the subclass constructor and must be the first statement.
Usage of Java super Keyword super can be used to invoke immediate parent class method. super() can be used to invoke immediate parent class constructor.
If we call "super()" without any superclass Actually, nothing will be displayed. Since the class named Object is the superclass of all classes in Java. If you call "super()" without any superclass, Internally, the default constructor of the Object class will be invoked (which displays nothing).
The main constructor must be called so any other constructor must call the main or another constructor that will call the main one. The constructor of the super is called in the main constructor as part of the inheritance declaration. This mean you can call only one super constructor.
class BaseClassExample(file: String, mode: String) {
def this(file: String) = this(file, "mode")
}
class ClassConstExample(file: String, header: String, inType: String, outType: String, flag: String, mode: String) extends BaseClassExample(file, mode) {
def this(file: String, header: String, inType: String, outType: String, flag: String) = this(file, header, inType, outType, flag, "mode")
def this(file: String, header: String, flag: String) = this(file, header, "inType", "outType", flag)
}
init
method to be called from every constructor (just in the main, or even init in the body directly)If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With