Re: Adding a method makes a class abstract?
Re: Adding a method makes a class abstract?
- Subject: Re: Adding a method makes a class abstract?
- From: Thomas Lachand-Robert <email@hidden>
- Date: Wed, 19 Mar 2003 13:10:34 +0100
Le lundi, 17 mars 2003, ` 12:27 Europe/Paris, Mark Patterson a icrit :
I'm going through the Hillegass book, and thought I'd try to change the
class used in one of the earlier exercises. So I subclassed NSNumber,
and when I ran it a line that worked with an instance of NSNumber gave
me an abstract object exception. Very strange from my Java / Delphi
point of view.
The code is:
#import <Foundation/Foundation.h>
@interface SimpleNumber: NSNumber {
}
- (NSString *)description;
@end
@implementation SimpleNumber
- (NSString *)description
{
return [[NSString alloc] initWithFormat:@"%d", [self intValue]];
}
@end
You can't easily subclass NSNumber, because it's a class cluster
(search for these words in the list or at cocoa.mamasam.com); in
particular, it's abstract hence the error. Anyway, there is no need
here, since contrarily to Java/C++, Obj-c allows you to overwrite or
define methods to any class. For instance:
@implementation NSNumber (additionalMethod)
-myAdditionalMethod (...) { }
@end
In general, try to refrain to subclass in Obj-C: most of the time,
there is no need to. This is one of the reason Obj-C is much more
productive that Java/C++.
Thomas Lachand-Robert
********************** email@hidden
<< Et le chemin est long du projet ` la chose. >> Molihre, Tartuffe.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.