Abstract base classes in Objective C
Abstract base classes in Objective C
- Subject: Abstract base classes in Objective C
- From: Andrew White <email@hidden>
- Date: Thu, 14 Apr 2005 15:39:32 +1000
I want to do the following:
// Exist only to provide a consistent interface
@interface AbstractBaseClass : NSObject
{
}
- (int) publicMethodOne;
- (void) publicMethodTwo: (int) x;
- (void) publicMethodThree: (int) x withModifier: (int) Y;
@end
// Provide common implementation support for children
@interface ConcreteBaseClass : AbstractBaseClass
{
int x;
int y;
}
@end
The trouble I am having is that the compiler wants me to define
publicMethodOne, publicMethodTwo: and publicMethodThree:withModifier in
AbstractBaseClass. Since AbstractBaseClass exists purely as specification,
there is no meaningful definition of such methods.
I can put in a definition of the form shown below, but in a complex
interface this results in a lot of unnecessary function declarations and
negates compile-time warnings for unimplemented functions in children.
@implementation AbstractBaseClass
- (int) publicMethodOne
{ /* assert an error */ }
- (void) publicMethodTwo: (int) x
{ /* assert an error */ }
- (void) publicMethodThree: (int) x withModifier: (int) Y
{ /* assert an error */ }
@end
Is there a way to tell the compiler "It's abstract, the definitions are
supposed to be missing"? I looked into protocols, but they seem to be
rather heavyweight and orthogonal to what I want. Unlike Java interfaces,
you don't seem to be able to treat a class that conforms to protocol
equivalently as if it were a class. But it's possible I have misunderstood
how to use them.
Categories allow me the opposite behaviour. I don't want to add methods to
an existing class; I want to add data members and definitions to an
existing specification.
Any suggestions? Are protocols what I'm looking for?
--
Andrew White
--------------------------------------------------------------------------
This email and any attachments may be confidential. They may contain legally
privileged information or copyright material. You should not read, copy,
use or disclose them without authorisation. If you are not an intended
recipient, please contact us at once by return email and then delete both
messages. We do not accept liability in connection with computer virus,
data corruption, delay, interruption, unauthorised access or unauthorised
amendment. This notice should not be removed.
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden