views:

287

answers:

2

Let's say I have some classes like this:

abstract class View(val writer: XMLStreamWriter) {
    // Implementation
}

class TestView(writer: XMLStreamWriter) extends View(writer) {
    // Implementation
}

Most subclasses of View are not going to take different constructor arguments. I would like to be able to write something like this:

class TestView extends View {
    // Implementation
}

Is there some shortcut to write subclasses so that you don't have to explicitly define the constructor args and pass them to the superclass (so that I don't have to re-write all my subclasses if I change the signature of the superclass)?

+4  A: 

I'm afraid you're on your own there. Constructors aren't inherited or polymorphic and subclass constructors, while they must and always do invoke a constructor for their immediate superclass, do not and cannot have that done automatically, except if there's a zero-arg constructor, which is implied by the mention of the superclass's name in an "extends" clause.

Randall Schulz

Randall Schulz
+3  A: 
abstract class View {
    def writer: XMLStreamWriter
    // Implementation
}

class TestView(val writer: XMLStreamWriter) extends View {
    // Implementation
}

Is this what you are looking for?

Mushtaq Ahmed