2

Possible Duplicate:
Class methods which create new instances

I feel like I'm missing something important. I'm creating a matrix class for matrix algebra as an abstract superclass, with different possibilities for entries in that matrix as its subclasses. My superclass is called Matrix, and my subclass is called FractionMatrix, where the entries are Fraction objects. So I have this convenience constructor:

+ (Matrix *)matrix { // creates an empty matrix object
    return [[Matrix alloc] init];
}

If in my code I do [FractionMatrix matrix], I would get a Matrix object rather than a FractionMatrix object. Is there some way of writing this method so that I don't need to override it in the subclass -- a self for classes, essentially?

While in this very simple case it's not a lot of work to simply override the method, there are more substantial methods for which overriding would represent pointless code duplication.

Community
  • 1
  • 1

0 Answers0