I know that abstract classes can't be instantiated, so maybe I'm answering my own question, but is there a way to put a private constructor in an abstract class? My problem is I have an abstract class with two subclasses that have the exact same methods, and some of the methods are done in the same way so I'd like to implement them in the abstract class, but they return a member of the class so I can't think of a way to return something I can't instantiate. I want to return this(x) and but I don't know if I can without the abstract class having a constructor.
abstract class Storage {
def +(other: Storage) : Storage = {
var d=List()
for (i<-other.length){
d=this.element(i)+other.element(i)
}
this(d)
}
}
Where element gives the element at the specified position, and the two subclasses use constructors that take a List as a paremeter.
What you're looking for is a concept named "virtual constructor" in some languages, such as Delphi. Scala doesn't have virtual constructors, but you can partially emulate them with a virtual factory method, which is sufficient for your use case: