To simplify my actual code let's say there are two classes, one a subclass of the other:
class Chair {
val canFold = false;
// ...
}
class FoldableChair extends Chair {
val canFold = true;
// ...
}
and in my implementation I will have potentially hundreds of other subclasses of Chair or FoldableChair:
class Armchair extends ... {}
class DeckChair extends ... {}
//... etc
For each of these subclasses, suppose each one has a lengthy implementation but I want to be able to have it sometimes extend Chair and sometimes extend FoldableChair - without duplicating the code. I'd like to do so without having the subclass itself be extended. Is this possible somehow? Do I need to use traits to do this?
I'd also like to be able to create particular instances of a subclass which sometimes extend Chair and sometimes extend FoldableChair, but that choice is made when instantiating it. Is this possible too? Thanks!
Edit: to clarify, what I really want is this:
class Armchair extends Chair {}
class ArmchairFoldable extends FoldableChair {}
but the implementation of Armchair and ArmchairFoldable are exactly the same. That is, I'd like to not duplicate their implementations.