Re: Abstract classes and methods
Re: Abstract classes and methods
- Subject: Re: Abstract classes and methods
- From: "Michael B. Johnson" <email@hidden>
- Date: Tue, 28 Aug 2001 22:40:56 -0700
On Tuesday, August 28, 2001, at 09:11 PM, stuartbryson
<email@hidden> wrote:
In C++ it is possible to force both classes and methods to be abstract.
That is classes that cannot be instantiated without being subclassed and
particular methods overrided. How do I force abstract classes and
methods in Obj-C.
what you're looking for is called a protocol in Objective-C. See Apple's
Objective C book for more info.
You basically say that your class "conforms to protocol A" where that
means that you promise to implement (and the compiler will check that you
do :-)) all the methods in the protocol. For example, I have a file
WK2DImagesHandler.h that declares a "formal protocol":
@protocol WK2DImagesHandler
- (NSArray*)images;
- (NSString*)nameForImage:(NSImage*)image;
- (NSImage*)imageForName:(NSString*)name;
...
@end
I then have a class that is a subclass of NSView and conforms to this
protocol and another (one called WKHotKeysHandler):
@interface WKImageView2D : NSView <WKHotKeysHandler, WK2DImagesHandler>
{
... code
}
@end
This gives you a "weak" form of multiple inheritance, where only one class
that you inherit from has any implementation. People (and Apple) talk
about "informal protocols", which means that they implement the methods
but don't declare the protocol as one that they conform to. I've never
understood why people do this, but Apple does this a lot...
I really like protocols.
--> Michael B. Johnson, Ph.D. -- email@hidden
--> Studio Tools, Pixar Animation Studios
-->
http://xenia.media.mit.edu/~wave