Let’s say I have two Classes like so:
Car
{
NSInteger wheels;
NSInteger bumpers;
}
+ (Car *)carWithData:(NSDictionary *)carData;
Lexus : Car
{
GPS *navigation;
}
+ (Lexus *)carWithData:(NSDictionary *)carData;
carWithData: is a simple helper method that creates an instance of Car populated with variables from carData. Lexus’ version would also set the navigation data.
How would Lexus’ carWithData look like without duplicating code from Car?
This is accomplished by calling
super‘s implementation ofinit…in the init method:Also note that both the
initWith…andcarWith…methods returnid, notCarorLexus.The way your code is set up you end up with casting problems, where
[Lexus carWithData:dataDict]does return an object of classLexus, but the compiler doesn’t know about it, as it expects aCar.